Jump to content

Using IF statement in WHERE clause?


GuitarGod

Recommended Posts

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 :happy-04:

Link to comment
https://forums.phpfreaks.com/topic/275222-using-if-statement-in-where-clause/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.