OR WHERE and AND in mySQL Query

Basically, I am trying to get data from SQL based on two different groups, t.type should be single , and t.status should be 1 , but as for t.org I want to do this get both DUAL and RA , that's what I tried to do to no avail.

 SELECT COUNT( p.tID ) FROM ticket AS t INNER JOIN people AS p ON t.ID = p.tID WHERE t.type = 'single' AND t.status='1' AND t.org = 'RA' OR t.org = 'DUAL' 

I'm sure their way of making this request work is simply not in my head

+4
source share
2 answers

AND has a higher precedence than OR , so your existing expression is currently evaluated as:

 WHERE (t.type = 'single' AND t.status='1' AND t.org = 'RA') OR t.org = 'DUAL' 

To force the use of alternative logic, explicit parentheses must be included:

 WHERE t.type = 'single' AND t.status='1' AND (t.org = 'RA' OR t.org = 'DUAL') 

However, in this case, you can use MySQL IN() instead of OR :

 WHERE t.type = 'single' AND t.status='1' AND t.org IN ('RA','DUAL') 
+15
source

You can use the IN condition:

 WHERE t.type = 'single' AND t.status = '1' AND t.org IN ('RA','DUAL') 

Or you can use parentheses to group conditions:

 WHERE t.type = 'single' AND t.status = '1' AND (t.org = 'RA' OR t.org = 'DUAL') 
+4
source

Source: https://habr.com/ru/post/1492336/


All Articles