MySQL OR OR logical query

I am in the middle of a project for my Comp Science project on Monday, and I came across a MySQL query question.

What I want to achieve is through words.

If the column to = jake and the column from = connor OR column to = connor and column from = jake get the cID from this particular table.

What I still have is

$query="SELECT cID FROM conversation WHERE to='$to' AND to='$from' OR to='$from' AND from='$to'"; 

What can I do to make this request work? Thanks in advance!

+4
source share
3 answers

You just need a pair of pairs () to group two conditional pairs separated by a logical OR

 $query=" SELECT cID FROM conversation WHERE /* to Jake, from Connor */ (`to`='$to' AND `from`='$from') /* OR to connor, from jake */ OR (`to`='$from' AND `from`='$to')"; 

Note: FROM and TO are a reserved MySQL keyword and therefore require quoting with reverse windows.

+11
source

This is untested, but I would have thought that you need to put to = '$ to' AND to = '$ from' (which should be from not guess?) In brackets

Just realized that someone else put this when I printed it

+1
source
 $query="SELECT cID FROM conversation WHERE (to='$to' OR to='$from' OR to='$from') AND from='$to'"; 
0
source

All Articles