Canman2005 Posted June 23, 2010 Share Posted June 23, 2010 Hi all I wonder if someone can help. Is it possible to add an "if" statement into a QUERY? For example, I have SELECT p.id, p.name, p.topic FROM people p WHERE p.type = 'Math'; But I want to add if(p.topic == 'today') { p.date } to the end of SELECT p.id, p.name, p.topic so it would look something like SELECT p.id, p.name, p.topic, if(p.topic == 'today') { p.date } FROM people p WHERE p.type = 'Math'; So in real terms it would end up looking like either SELECT p.id, p.name, p.topic FROM people p WHERE p.type = 'Math'; or if topic equaled "today" then it would use SELECT p.id, p.name, p.topic,p.date FROM people p WHERE p.type = 'Math'; I know the above is not possible, but what would I change it to if at all possible. Thanks all Ed Quote Link to comment Share on other sites More sharing options...
Mchl Posted June 23, 2010 Share Posted June 23, 2010 SELECT p.id, p.name, p.topic,IF(p.topic = 'today',p.date,NULL) AS date FROM people p WHERE p.type = 'Math'; Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted June 24, 2010 Author Share Posted June 24, 2010 thanks, that seemed to work great Just another question, but is it also possible to add something like that at the end of the QUERY, so adding JOIN t.things ON t.userid = p.id to the end of SELECT p.id, p.name, p.topic FROM people p WHERE p.type = 'Math'; so I guess it would be something like SELECT p.id, p.name, p.topic FROM people p (p.topic = 'yesterday', JOIN t.things ON t.userid = p.id) WHERE p.type = 'Math'; Thanks very much Quote Link to comment Share on other sites More sharing options...
Mchl Posted June 24, 2010 Share Posted June 24, 2010 Nope. You can however do: SELECT p.id, p.name, p.topic, t.things FROM people p LEFT JOIN otherTable AS t ON (t.userid = p.id AND p.topic = 'yesterday') WHERE p.type = 'Math'; which would essentialy have same result. Quote Link to comment Share on other sites More sharing options...
Canman2005 Posted June 24, 2010 Author Share Posted June 24, 2010 thanks dude, worked a charm Quote Link to comment 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.