Jump to content

CONDITIONAL statement using OR and AND


jmichael68

Recommended Posts

I have a problem when trying to query the table closed_mbl.  I want to return the COUNT of all records that contain the locations below while excluding all these records that contain anything else besides AIR.  How would I do this?  Right now it returns the count of everything from all these locations.  The field "method" is ignored.

 

SELECT COUNT(*) FROM closed_mbl WHERE location = 'ALL' OR location = 'AUCKLAND' OR location = 'BRAZIL' OR location = 'DALIAN' OR location = 'EXPORT' OR location = 'GALWAY' OR location = 'ISUNICOV' OR location = 'KOLIN' OR location = 'SGALWAY' OR location = 'SHANNON' OR location = 'SOMI' OR location = 'SUZHOU' OR location = 'UNICOV' OR location = 'WUJIANG' OR location = 'WUJIANGCC' AND method = 'AIR'

Link to comment
https://forums.phpfreaks.com/topic/211185-conditional-statement-using-or-and-and/
Share on other sites

Use parentheses:

 

SELECT COUNT(*) FROM closed_mbl
WHERE (location = 'ALL' OR location = 'AUCKLAND' OR location = 'BRAZIL' OR location = 'DALIAN' OR location = 'EXPORT'
OR location = 'GALWAY' OR location = 'ISUNICOV' OR location = 'KOLIN' OR location = 'SGALWAY' OR location = 'SHANNON'
OR location = 'SOMI' OR location = 'SUZHOU' OR location = 'UNICOV' OR location = 'WUJIANG' OR location = 'WUJIANGCC')
AND method = 'AIR'

 

Or use IN:

 

SELECT COUNT(*) FROM closed_mbl
WHERE location IN
('ALL','AUCKLAND','BRAZIL','DALIAN','EXPORT','GALWAY','ISUNICOV','KOLIN','SGALWAY','SHANNON','SOMI','SUZHOU','UNICOV','WUJIANG','WUJIANGCC')
AND method = 'AIR'

 

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.