mardi 19 janvier 2016

IF condition in WHERE clause in a mysql query

I have a query which is used to select the shift start time of a user from a table based on the user_id and the time then. Here I have an if condition to check whether the time he checks in is less than 1 hour early or less than 4 hours late. So the query is supposed to return the shift_strt_time if he is not more than 1 hour early and not more than 4 hours late. I want to know whether this is the right way to do it.

SELECT shift_strt_time 
FROM shift_attendance 
WHERE user_id = $user_id 
AND if($now >= shift_strt_time,if($now - shift_strt_time < 4*60*60,1,0),if(shift_strt_time - $now  <= 1*60*60,1,0)) 

And one more doubt I have is that, first I wrote the if condition like this,

if($now >= shift_strt_time,$now - shift_strt_time < 4*60*60,shift_strt_time - $now  <= 1*60*60) 

The query worked but I was in doubt about the right syntax of IF inside where clause. Please throw some light on it too.

Aucun commentaire:

Enregistrer un commentaire