Database command to delete users according to time definitions - mysql

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");

Related

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 to check does user is not active more than 1 minute

Hello I tried to update a row in table if somebody is offline more than 1 minute, and set LoggedIn then as 0, but my SQL query doesn't work, what I do wrong?
"UPDATE acc SET LoggedIn='0' WHERE LastTimeActive<(NOW(), INTERVAL 1 MINUTE)";
You are missing date_sub():
UPDATE acc
SET LoggedIn = '0'
WHERE LastTimeActive < date_sub(NOW(), INTERVAL 1 MINUTE);

Update Statement with Case in MYSQL

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 ...

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 ?

why not pulling data for last week

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'