fetching correct row in mysql query using AND OR -


need fetch specific row table query returning multiple rows

mysql

select distinct         id, sender_id, msg, name, profilepic, cur_time, cur_date     `chat`             (sender_id = '1' , receiver_id = '3')         or         (sender_id = '3' , receiver_id = '1')         ,         chat.id > '3' 

table sample:

id | sender_id | receiver_id | msg | name | profilepic 1      1              3        hi    jay     o.jpg 2      1              3        hey   jay     o.jpg 3      3              1        hi    tom     o.jpg 4      1              3        yes   jay     o.jpg 

the row expect query 4th id "4" query kept returning several other rows

or has lower precedence and, surround or expression in parentheses:

where (      (sender_id = '1' , receiver_id = '3')      or (sender_id = '3' , receiver_id = '1')    )   , chat.id > '3' 

consider original where clause:

where     (sender_id = '1' , receiver_id = '3')     or     (sender_id = '3' , receiver_id = '1')     ,     chat.id > '3' 

because and takes precedence on or, interpreted follows:

   (sender_id = '1' , receiver_id = '3')     or     ((sender_id = '3' , receiver_id = '1') , chat.id > '3') 

the parentheses proposed in answer force 2 or expressions evaluated together, this:

  ((sender_id = '1' , receiver_id = '3') or (sender_id = '3' , receiver_id = '1'))    ,    chat.id > '3' 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -