Jump to content

Recommended Posts

How can I have a mysql query with 3 (or more) Where clauses (I know Where only takes 2). The following code doesn't work, but it demonstrates what I'd like to achieve:

 

SELECT msgSender, message_content.msgId, msgDate, msgContent
FROM utility.message_inbox, utility.message_content
WHERE msgRecipient = "o4jd9sk1"
AND message_inbox.msgId = message_content.msgId
AND message_content.msgChild = "NULL" 

Link to comment
https://forums.phpfreaks.com/topic/142002-solved-multiple-where-conditions/
Share on other sites

Use parentheses?

 

SELECT msgSender, message_content.msgId, msgDate, msgContent
FROM utility.message_inbox, utility.message_content
WHERE 
(msgRecipient = "o4jd9sk1" AND message_inbox.msgId = message_content.msgId)
AND message_content.msgChild = "NULL"

 

or

 

 

SELECT msgSender, message_content.msgId, msgDate, msgContent
FROM utility.message_inbox, utility.message_content
WHERE 
msgRecipient = "o4jd9sk1" AND (message_inbox.msgId = message_content.msgId
AND message_content.msgChild = "NULL")

 

it's all about precedence.

I used parentheses as shown below:

SELECT msgSender, message_content.msgId, msgDate, msgContent
FROM utility.message_inbox, utility.message_content
WHERE
msgRecipient = "o4jd9sk1" AND (message_inbox.msgId = message_content.msgId
AND message_content.msgChild = "NULL")

 

but an empty result set was returned. It seems that the cause of the problem is the: AND message_content.msgChild = "NULL" statement. When I changed the aforementioned statement to: AND message_content.msgId = "1" the result set showed the expected content.

 

Currently, the msgChild field is by default set to NULL, it's supposed to indicate that the current message is the last one in the thread since it has no children. Is the error in my statement or elsewhere?

Yeah it probably would make more sense to use OR with parentheses.

SELECT msgSender, message_content.msgId, msgDate, msgContent
FROM utility.message_inbox, utility.message_content
WHERE
(msgRecipient = "o4jd9sk1" AND message_inbox.msgId = message_content.msgId)
OR message_content.msgChild = "NULL"

 

@Mchl, I'm not too sure.  the op said he wasn't able to use 3 where clauses so the first thing that came to mind was..Parentheses!  I spoke to soon I guess.  I'm definitely not the brightest SQL tool in the shed, but I can make my way around.

Logical conjunction (AND) and alternative (OR) are both associative

[tex]a \land (b \land c) \equiv (a \land b) \land c [/tex]

[tex]a \lor (b \lor c) \equiv (a \lor b) \lor c [/tex]

so as long as there are only ORs or only ANDs, there is no need to use parentheses.

 

(tex code shamelessly ripped off Wikipedia :P )

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.