Reputation: 155
Good day, I am a newbie programmer. I would like to ask what is the problem with my code? All I want sir/ma'am to view the user with letter "a" in firstname,middlename and lastname and status is equal to 1 and usertype is not equal to the word "Admin".
The code below showing other user record, even the usertype is Admin. Please help me sir.
This is my code:
SELECT * FROM users WHERE usertype != 'Admin' AND firstname LIKE '%a%' OR middlename LIKE '%a%' OR lastname LIKE '%a%' AND userstatus = '1'
Please help me. I see some post here that using multiple like, Sorry if my english is too bad. Thanks in adavance
Upvotes: 0
Views: 882
Reputation: 3887
I assume you want to group your LIKE
statements. You can use brackets for that, like so:
SELECT * FROM users
WHERE usertype != 'Admin'
AND (
firstname LIKE '%a%'
OR middlename LIKE '%a%'
OR lastname LIKE '%a%'
)
AND userstatus = '1'
Or put them the way you want. However I would recommend you to prevent statements like this, because the can become very ill performing, especially when the wildcharacters are on both sides. But I assume that is just as an example here.
EDIT: See here for fiddle.
Upvotes: 3
Reputation: 6218
SELECT * FROM users WHERE usertype != 'Admin' AND (firstname LIKE '%a%' OR middlename LIKE '%a%' OR lastname LIKE '%a%') AND userstatus = '1'
Upvotes: 1