I have a MySQL table like below:
create table login_schedule(name varchar(20),emp_id varchar(5),login_time datetime,logout_time datetime);
and data insertion looks like below:
insert into login_schedule values('Ram','01',now(),now()+interval 30 minute);
So, I want to perform a update query only if one select statement gives empty set result.
Below is my select statement:
select name,emp_id,logout_time,login_time from login_schedule where emp_id='01' and now()<date_format(logout_time,'%Y-%m-%d %H:%i:%s');
My Update statement is like below:
update login_schedule set logout_time=(select date_format(now() + interval 30 minute,'%Y-%m-%d %H:%i:%s') from dual) where emp_id='01';
So, is there any way please help. Mainly what I have to achieve is, if employee perform any action before his allocated logout time (which is 30 minutes after login time), then his logout time will increases to another 30 minutes. If users stays idle for 30 minutes then he can’t perform any action, he have to login again to get another 30 minutes.
So, to achieve this first select query must return empty set then only update query will be performed, else if select query gives any output then update query will not going to perform.
Please help.
2
Answers
I don’t think you need to do the SELECT query at all. What you described is that you want to extend the logout time to NOW() + 30 minutes if and only if that time has not yet passed.
This update would accomplish that: