webmaster1 Posted April 21, 2009 Share Posted April 21, 2009 Hi All, I'm using a basic select query in my php page on two tables at the same time. You'll notice I've used an if, then, else condition succesfully: IF( tablename1.column1 IS NULL , 'Value', tablename1.column1 ) AS columnname SELECT * FROM ( SELECT masterdata.masterdataid, masterdata.companyname, masterdata.contactname, IF( resultsnotations.prospectrating IS NULL , 'Not Rated', resultsnotations.prospectrating ) AS prospectrating, resultsnotations.lastactivity FROM masterdata LEFT JOIN resultsnotations ON masterdata.masterdataid = resultsnotations.masterdataid WHERE category = 'CIF' ORDER BY masterdata.masterdataid, resultsnotations.lastactivity DESC ) AS a GROUP BY masterdataid The problem occurs when I try to use a similar condition for the lastactivity column. I get the following error: MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF( resultsnotations.lastactivity IS NULL , 'Loaded', resultsnotations.lastactiv' at line 9 The only thing different this time around is the fact that I'm calling a value from the second table rather than the first. Can somebody please help me spot where I'm going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/155064-solved-basic-select-query-using-if-else/ Share on other sites More sharing options...
Mchl Posted April 21, 2009 Share Posted April 21, 2009 Using IFNULL may not help here, but will certainly look better. And check for any missing commas before this IF Quote Link to comment https://forums.phpfreaks.com/topic/155064-solved-basic-select-query-using-if-else/#findComment-815582 Share on other sites More sharing options...
webmaster1 Posted April 21, 2009 Author Share Posted April 21, 2009 Thanks mchl. That's a lot easier on the eye. I discvovered the issue while making those changes too (typos). Functioning code: SELECT * FROM ( SELECT masterdata.masterdataid, masterdata.companyname, masterdata.contactname, IFNULL( resultsnotations.prospectrating, 'Not Rated') AS prospectrating, IFNULL( resultsnotations.lastactivity, 'Loaded') AS lastactivity FROM masterdata LEFT JOIN resultsnotations ON masterdata.masterdataid = resultsnotations.masterdataid WHERE category = 'CIF' ORDER BY masterdata.masterdataid, resultsnotations.lastactivity DESC ) AS a GROUP BY masterdataid Quote Link to comment https://forums.phpfreaks.com/topic/155064-solved-basic-select-query-using-if-else/#findComment-815590 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.