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 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted March 4, 2013 Solution 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 Quote Link to comment 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! 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.