prgrm
prgrm

Reputation: 3833

Filtering by comparing 2 rows in SQL

I have a table like this:

item consumerID userID
A        1        1
B        1        1
C        1        2
D        2        2
E        2        2
F        2        3
G        4        4
H        5        6

I want to get all items where consumerID is not like userID grouped by userID

I am currently doing it programatically in PHP but I wonder if it'd be possible to do this with SQL directly. I am using MariaDB.

Desired output:

item consumerID userID
C        1        2
F        2        3
H        5        6

Upvotes: 0

Views: 40

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

Are you simply looking for "not equals"?

select t.*
from t
where consumerId <> userId;

Upvotes: 2

Related Questions