why not pulling data for last week - mysql

Why wouldn't this be working for pulling data from last week?
if($_GET['reminder'] == 'lastweek') {
$lastweek = date('Y-m-d', strtotime('last sunday'));
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$lastweek' AND '$lastweek' - INTERVAL 6 DAY ORDER BY contacttodo.reminder ASC";

This is the definition of BETWEEN:
expr BETWEEN min AND max
so you should put the smaller value (minimum) first:
BETWEEN '$lastweek' - INTERVAL 6 DAY AND '$lastweek'
'$lastweek' - INTERVAL 6 DAY ORDER is smaller than '$lastweek'

Related

Database command to delete users according to time definitions

Users for whom the registration date (timestamp) is older than today-1day and fulfills the condition should be deleted.
Example:
User 1 to 3 have RegDate: 11/21/2022 - HH:MM
User 4 have RegDate: 11/18/2022 - HH:MM
a) RegDate = 11/18/2022 (is stored in the database as a timestamp)
b) Today is 11/22/2022 - 1 day = 11/21/2022 = HH:MM
Since the registration date is before 11/21/2002 and the condition of user 4 is met, he should be deleted.
My attempt:
$db->query("DELETE FROM ".TABLE_PREFIX."users WHERE (regdate < (UNIX_TIMESTAMP() - '86400')) AND hideemail = '1'");
oder
$db->query("DELETE FROM ".TABLE_PREFIX."users WHERE (datediff(now(), `regdate`) >= 1) AND hideemail = '1'");
Regardless of whether I choose < or >, users 1-3 are always deleted but user 4 is not.
The aim should be that only user 4 is deleted and users 1 to 3 are not.
I'm defining something wrong.
Can anyone tell me what I am doing wrong?
Thanks in advance.
Do you want to delete users registered before 2022-11-21 (CURRENT_DATE - INTERVAL 1 DAY) or before 2022-11-21 13:10:17 (NOW() - INTERVAL 1 DAY)
$db->query("DELETE FROM " . TABLE_PREFIX . "users WHERE (regdate < CURRENT_DATE - INTERVAL 1 DAY) AND hideemail = 1");
or
$db->query("DELETE FROM " . TABLE_PREFIX . "users WHERE (regdate < NOW() - INTERVAL 1 DAY) AND hideemail = 1");

Query database for Current Week Results [duplicate]

I have table temp with structure on sqlfiddle:
id(int 11 primary key)
name(varchar 100)
name2(varchar 100)
date(datetime)
I would like get record on this week, example if now 21.11.2013 i would like all rows on 18.11.2013 to 24.11.2013(on week)
Now I see next algorithm:
obtain weekday
calculate how many days ago was Monday
calculate the date Monday
calculate future date Sunday
make a request on date
Tell me please, is exist a shorter algorithm (preferably in the query MySQL)?
ADD Question is: Why this query select record on date 17.11.2013(Sunday) - 23.11.2013(Saturday) and how get records on date 18.11.2013(Monday) - 24.11.2013(Sunday) ?
query:
select * from temp
where yearweek(`date`) = yearweek(curdate())
Thanks!
Use YEARWEEK():
SELECT *
FROM your_table
WHERE YEARWEEK(`date`, 1) = YEARWEEK(CURDATE(), 1)
Use YEARWEEK. If you use WEEKOFYEAR you will get records of previous years also.
SELECT id, name, date
FROM table
WHERE YEARWEEK(date)=YEARWEEK(NOW());
For selecting records of day, week and month use this way:
function my_func($time, $your_date) {
if ($time == 'today') {
$timeSQL = ' Date($your_date)= CURDATE()';
}
if ($time == 'week') {
$timeSQL = ' YEARWEEK($your_date)= YEARWEEK(CURDATE())';
}
if ($time == 'month') {
$timeSQL = ' Year($your_date)=Year(CURDATE()) AND Month(`your_date`)= Month(CURDATE())';
}
$Sql = "SELECT * FROM your_table WHERE ".$timeSQL
return $Result = $this->db->query($Sql)->result_array();
}
You can do it by following method
SELECT DATE_FORMAT("2017-06-15", "%U");
Get number of week (From 00 to 53 )
Where (Sunday 1st day and Monday last day of week)
May be useful for you.
example:
SELECT DISTINCT DATE_FORMAT('dates', '%U') AS weekdays FROM table_name;
The short solution would be this:
SELECT * FROM my_table WHERE DATE_FORMAT("2021-08-19 15:40:33", "%U") = WEEK(CURDATE());

Showing data from database 15 mnute earlier and stop showing it 15 minute late from the time set for it

$query = "SELECT title,time_strt,ampmstrt,time_end,ampmend
FROM addfunction
WHERE (NOW() + INTERVAL 15 MINUTE) BETWEEN time_strt AND time_end AND location = 'MAWAR ROOM'";
topo,
i hope i understand it correctly...
select title,time_strt,ampmstrt,time_end,ampmend
FROM addfunction
where UNIX_TIMESTAMP(time_strt) >= (UNIX_TIMESTAMP(NOW()) - 15 * 60)
and UNIX_TIMESTAMP(time_end) >= (UNIX_TIMESTAMP(NOW()) + 15 * 60)
AND location = 'MAWAR ROOM';
This will found all rows start 15 minutes before time_strt and ends 15 minutes after time_end.
Ended doing it like this which show the data from database 15 minute earlier than time_strt and stop Showing after 15 minute have elapsed from the time_end. I changed the data type from DATETIME to TIME. Thanks to evryone and mostly #harley81!
WHERE date = CURDATE() AND time_strt <= (CURTIME() + INTERVAL 15 MINUTE) AND time_end >= (CURTIME() - INTERVAL 15 MINUTE) AND location = 'MAWAR ROOM'";

How can I query for +/- 10 days TheDate in DB query

Okei,, so I am trying to retrieve some info from my database with a +/- 10 days setting.
Values:
Event.start_date = y-m-d
Event.end_date = y-m-d
My.date = y-m-d
$conditions["My.date >="] = date( 'Y-m-d');
$conditions["My.date -10 >="] = Event.start_date;
$conditions["My.date +10 >="] = Event.end_date;
Any suggestions how I can achive this?
Try this in your query
If you are checking against today's date:
WHERE startDate BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 10 DAY)
If you are checking against your own end date:
WHERE startDate BETWEEN endDate() AND DATE_ADD(endDate(), INTERVAL 10 DAY)
For going back - 10 days
WHERE startDate BETWEEN DATE_ADD(startDate(), INTERVAL -10 DAY) AND endDate()
Please adjust logic according to your needs.
This is how :)
$conditions = array(
"OR" => array(
array('My.date BETWEEN DATE_ADD(?, INTERVAL -10 DAY) AND ?' => array($data['Event']['start_date'],$data['Event']['end_date'])),
array('My.date BETWEEN ? AND DATE_ADD(?, INTERVAL 10 DAY)' => array($data['Event']['start_date'],$data['Event']['end_date']))
)
);

Whats wrong with my MYSQL QUERY?

Can someone please tell me what is wrong with my query.
I am trying to fetch data fro my table based on the column called weekend, if weekend is set to "0" show from Sunday 6pm until Friday 9pm, then if weekend is set to "1" Show from Friday 9pm until Sunday 6pm.
SELECT *
FROM closures
WHERE closures.weekend = 0
OR WEEKDAY(NOW()) < 4
AND closures.weekend = 1
OR WEEKDAY(NOW()) > 4
OR (WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR (WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
It helps to phrase the question properly. What you should have said is "between 9pm Friday and 6pm Sunday I want to show the rows where closures.weekend = 1, otherwise show those where closures.weekend = 0".
Hence what you need to do is generate a value of 1 or 0 depending on whether it's the weekend or not, and then SELECT those rows where weekend has that value, i.e:
SELECT *
FROM
closures
WHERE
weekend = IF(
(WEEKDAY(NOW()) = 4 AND HOUR(NOW()) >= 21)
OR (WEEKDAY(NOW()) = 5)
OR (WEEKDAY(NOW()) = 6 AND HOUR(NOW()) < 18)
, 1, 0)
Weekend cant be both, 0 and 1
SELECT *
FROM
closures
WHERE
closures.weekend = 0
OR
(
WEEKDAY(NOW()) < 4
AND closures.weekend = 1
)
OR WEEKDAY(NOW()) > 4
OR
(WEEKDAY(NOW())=4 AND HOUR(NOW())>21)
OR
(WEEKDAY(NOW())=6 AND HOUR(NOW())<18)
My first guess is that the
SELECT field1, field2, ...
is missing ?