aron pascua aron pascua - 4 months ago 4
PHP Question

mysql query does not display the right output

I have this

mysqli
query that will display all the data that the
crew_status
is
PENDING FOR LINEUP
but with my
query
right now, it displays all the
data
that is in
database


Here is my
query
:

SELECT * FROM `crew_info` WHERE `crew_rank` = 'MASTER' OR `crew_rank` = 'CHCK' OR `crew_rank` = 'MESSMN' OR `crew_rank` = '2E' OR `crew_rank` = '2M' OR `crew_rank` = 'COOK' OR `crew_rank` = 'OLR' OR `crew_rank` = 'AB' OR `crew_rank` = '3E' OR `crew_rank` = '3M' AND `crew_status` = 'PENDING FOR LINEUP'


Can someone tell what is the problem?

JPG JPG
Answer

Wrap your or with (, ), try this:

SELECT *
FROM `crew_info`
WHERE (
    `crew_rank` = 'MASTER'
    OR `crew_rank` = 'CHCK'
    OR `crew_rank` = 'MESSMN'
    OR `crew_rank` = '2E'
    OR `crew_rank` = '2M'
    OR `crew_rank` = 'COOK'
    OR `crew_rank` = 'OLR'
    OR `crew_rank` = 'AB'
    OR `crew_rank` = '3E'
    OR `crew_rank` = '3M')
AND `crew_status` = 'PENDING FOR LINEUP'

Without (), if one of these ORs matches, then condition left will not be computed.

See this:

true || false && false = true 
(true || false) && false = false

And I think you'd better use IN instead of OR like :

SELECT *
FROM `crew_info`
WHERE 
    `crew_rank` IN ('MASTER','CHCK','MESSMN','2E','2M','COOK','OLR','AB','3E','3M')
AND `crew_status` = 'PENDING FOR LINEUP'
Comments