mastubbs Posted May 28, 2013 Share Posted May 28, 2013 Hi all, So im quite new to sql queries and ive been reading how to use if else but i ccant figure out what im getting wrong in this one. Can anyone help? I want to use a datetime variable (which is an automatic timestamp 'datetime') but only if the user has not manually over-ridden this by entering their own datetime ('datetime_man). I have tried to use IF ELSE but im getting it wrong somewhere. Is the syntax right? $Find_Query1 = mysql_query("SELECT DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s') AS datetime2 IF datetime_man='null' ELSE DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s') AS datetime2, DATE_FORMAT(datetime,'%H:%i on %d/%m/%y') AS datetime3 IF datetime_man='null' ELSE DATE_FORMAT(datetime,'%H:%i on %d/%m/%y') AS datetime3, SBP, SBP_R, SBP_B, DBP, DBP_R, DBP_B, HR, HR_R, HR_B, RR, RR_R, RR_B, SpO2, SpO2_R, SpO2_B, O2flow, O2flow_R, O2flow_B, O2device, O2deviceOther, TEMP, TEMP_R, TEMP_B, AVPU FROM addobs WHERE mrn='$Search' AND DATE(datetime)='$Date' AND NOT hidden = 'yes' order by datetime2 ASC"); Thanks in advance for the help, Matt Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/ Share on other sites More sharing options...
Jessica Posted May 28, 2013 Share Posted May 28, 2013 IF condition do this ELSE condition do this other thing Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432780 Share on other sites More sharing options...
kicken Posted May 28, 2013 Share Posted May 28, 2013 (edited) Assuming datetime_man is set to NULL when it hasn't been entered, then you can just use the COALESCE function. SELECT DATE_FORMAT(COALESCE(datetime_man, datetime),'%Y/%m/%d %H:%i:%s') AS datetime2 , DATE_FORMAT(COALESCE(datetime_man, datetime),'%H:%i on %d/%m/%y') AS datetime3 , SBP, SBP_R, SBP_B, DBP, DBP_R, DBP_B, HR, HR_R, HR_B, RR, RR_R, RR_B, SpO2, SpO2_R, SpO2_B, O2flow, O2flow_R, O2flow_B, O2device, O2deviceOther, TEMP, TEMP_R, TEMP_B, AVPU FROM addobs WHERE mrn='$Search' AND DATE(datetime)='$Date' AND NOT hidden = 'yes' order by datetime2 ASC"); Edited May 28, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432784 Share on other sites More sharing options...
mastubbs Posted May 28, 2013 Author Share Posted May 28, 2013 (edited) Assuming datetime_man is set to NULL when it hasn't been entered, then you can just use the COALESCE function. SELECT DATE_FORMAT(COALESCE(datetime_man, datetime),'%Y/%m/%d %H:%i:%s') AS datetime2 , DATE_FORMAT(COALESCE(datetime_man, datetime),'%H:%i on %d/%m/%y') AS datetime3 , SBP, SBP_R, SBP_B, DBP, DBP_R, DBP_B, HR, HR_R, HR_B, RR, RR_R, RR_B, SpO2, SpO2_R, SpO2_B, O2flow, O2flow_R, O2flow_B, O2device, O2deviceOther, TEMP, TEMP_R, TEMP_B, AVPU FROM addobs WHERE mrn='$Search' AND DATE(datetime)='$Date' AND NOT hidden = 'yes' order by datetime2 ASC"); Kicken you legend that did the trick. For future reference what was wrong with my IF ELSE syntax? Im guessing from Jessica it should have been "SELECT IF datetime_man='null' DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s') AS datetime2 ELSE DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s') AS datetime2 Edited May 28, 2013 by mastubbs Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432788 Share on other sites More sharing options...
Jessica Posted May 28, 2013 Share Posted May 28, 2013 *shrug* did you try it? Coalesce is definitely better. Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432793 Share on other sites More sharing options...
kicken Posted May 28, 2013 Share Posted May 28, 2013 For one, to compare something to NULL you have to use IS NULL or IS NOT NULL. Using = or != will not work since null is not any particular value, but rather the concept of "no value". Second, I'm not as familar with mysql anymore so not sure if it supports that type of if/else syntax. I know there is the IF() function which you'd use as SELECT IF(datetime_man IS NOT NULL, DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s'), DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s')) or, the way I do it for sql server using CASE would work also: SELECT CASE WHEN datetime_man IS NOT NULL THEN DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s') ELSE DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s') END Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432794 Share on other sites More sharing options...
mastubbs Posted May 28, 2013 Author Share Posted May 28, 2013 For one, to compare something to NULL you have to use IS NULL or IS NOT NULL. Using = or != will not work since null is not any particular value, but rather the concept of "no value". Second, I'm not as familar with mysql anymore so not sure if it supports that type of if/else syntax. I know there is the IF() function which you'd use as SELECT IF(datetime_man IS NOT NULL, DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s'), DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s')) or, the way I do it for sql server using CASE would work also: SELECT CASE WHEN datetime_man IS NOT NULL THEN DATE_FORMAT(datetime_man,'%Y/%m/%d %H:%i:%s') ELSE DATE_FORMAT(datetime,'%Y/%m/%d %H:%i:%s') END Ah ok, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432801 Share on other sites More sharing options...
mastubbs Posted May 28, 2013 Author Share Posted May 28, 2013 *shrug* did you try it? Good idea. I will do that. You will be pleased to hear my tables are no longer named "table_" on your advice by they way. Quote Link to comment https://forums.phpfreaks.com/topic/278484-sql-query-syntax-wrong/#findComment-1432802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.