Muhammed Ashik Muhammed Ashik - 3 months ago 9
MySQL Question

Multiple AND/OR logic messed up

I need to get my data when

userID=4
and status= In use or Pending or Deleted.
But I am getting extra data not sure why.

I am getting extra data from

userid=3


MySQL query is:

SELECT * FROM `registered_bicycle`
WHERE (`userID`='4')
AND (`status`='In Use')
OR (`status`='Pending')
OR (`status`='Deleted')


enter image description here

Answer

Try this instead:

SELECT
    *
FROM
    `registered_bicycle`
WHERE `userID` = '4'
AND `status` IN('In Use','Pending','Deleted')

Your and/or logic got mixed up because of lacking proper parentheses. Also you can use IN instead of multiple OR clauses.

Btw, IN is just the short form of OR