GuitarGod Posted March 4, 2013 Share Posted March 4, 2013 Hi Guys, I'm having a little trouble trying to use an IF statement in the WHERE clause of a mysql. What I'm trying to achieve, is that if m.message_type is set to 1, then I would like the m.message_id to match mt.message_id, otherwise I want mt.message_id to equal -1. I'm sorry if I've overcomplicated it, I'm just not sure how it's written in SQL. SELECT m.message_id, m.message_type, mt.message_id, mt.message_text FROM messages m, message_text mt WHERE ( if m.message_type // // etc ) Any help is, as always, fully appreciated Link to comment https://forums.phpfreaks.com/topic/275222-using-if-statement-in-where-clause/ Share on other sites More sharing options...
Barand Posted March 4, 2013 Share Posted March 4, 2013 Is this what you were trying to do SELECT m.message_id, m.message_type, IF(m.message_type=1, mt.tmessage_id, -1) as mtid, mt.message_text FROM messages m LEFT JOIN message_text mt ON m.message_id = mt_message_id AND m.message_type = 1 Link to comment https://forums.phpfreaks.com/topic/275222-using-if-statement-in-where-clause/#findComment-1416548 Share on other sites More sharing options...
GuitarGod Posted March 4, 2013 Author Share Posted March 4, 2013 That's exactly what I was trying to do, your code has worked fine Thank you very much! Link to comment https://forums.phpfreaks.com/topic/275222-using-if-statement-in-where-clause/#findComment-1416584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.