JohnWick
JohnWick

Reputation: 5149

SQLite "<>" operator not working

I have this query:

SELECT username FROM users INNER JOIN friends WHERE friends.user_a = users.id OR friends.user_b = users.id AND users.username <> 'david';

It is returning a row where users.username = 'david'. How can I have this not returned?

'users' table structure/sample data enter image description here

'friends' table structure/sample data enter image description here

enter image description here

Upvotes: 0

Views: 47

Answers (1)

Baha
Baha

Reputation: 408

Maybe try NOT LIKE '%david%' for any non-print chars?

try WHERE (friends.user_a = users.id OR friends.user_b = users.id) AND users.username <> 'david';

Explanation: Your logic was friends.user_a = users.id OR friends.user_b = users.id AND users.username <> 'david' You were saying: "bring me anyone who's friend with user_a OR etc..." so when the first applies it doesn't need to check the rest, but after the parentheses, we are saying: "Bring me anyone who 1. Is friends with user_a or users_b 2. His user_name is not 'david'".

Upvotes: 2

Related Questions