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 ?
Related
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");
I am trying to indicate all rows with last week's data (Monday-Sunday) using the following, gives me the data from (Tue-Mon) 2 feb-8 feb - so it is dynamic depending on the day of this week and tomorrow will give me the data from Wed-Tue. Any idea how to fix this:
case when `Date` >= date_sub(now(),INTERVAL 1 WEEK) and
`Date`< (date_sub(now(),INTERVAL 1 WEEK)+7) then 1 else 0 end
In mysql you could try using week() checking for the same week of curdate()
case when week(`Date`) = weeek(curdate())
AND year(`Date`) = year(curdate()) then 1 else 0 end
or for the previous week
case when week(`Date`) = weeek(curdate())-1
AND year(`Date`) = year(curdate()) then 1 else 0 end
I'm creating a fixture list for football and would like to create a page "weekend fixtures".
I have managed the following
SELECT * FROM events WHERE DAYOFWEEK(event_time) = 7
or DAYOFWEEK(event_time) = 1
or (DATE_FORMAT(event_time, "%T") > '17:30:00' AND DAYOFWEEK(event_time) = 6)
The above code works in returning fixtures for all weekends. But I need either the current or next.
Any advice would be appreciated.
you can use WEEK function from mysql
This would check id´f event timestamp is in this or the next week
SELECT * FROM events WHERE (DAYOFWEEK(event_time) = 7
or DAYOFWEEK(event_time) = 1
or (DATE_FORMAT(event_time, "%T") > '17:30:00' AND DAYOFWEEK(event_time) = 6))
AND ((WEEK(event_time) = WEEK(Now()) ) OR (WEEK(event_time) = WEEK(Now()) +1 ))
I have this issue and I have investigated more than five house but find nothing :( . I have table called support.
UPDATE support s SET s.Survey_Status = 0
CASE
WHEN s.Survey_Status = 0 Then 1
WHEN s.Survey_Status = 1 Then 2
End
Where last_response < ADDDATE(CURDATE(), INTERVAL 1 DAY)
and support_waiting ="n" ;
I need to update the support table and set the survey_status =1 except the fifth row in the table will be =2 . For example, if I have survey_ status from 1 to 10 = 1 then the fifth will =2 . any idea please ??
By the way, I am working with mysql Heidi .
Thanks in Advance
You can combine user variables and MOD():
UPDATE support, (SELECT #r:=0) init
SET Survey_Status = IF(MOD(#r:=#r+1,5), 1, 2)
WHERE last_response < CURRENT_DATE + INTERVAL 1 DAY
AND support_waiting = 'n'
ORDER BY ...
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'