Konsta05
Konsta05

Reputation: 1

How to find only id that has different values in MySQL?

I have a table with two columns:

id num
1 2
2 8
1 7
7 3

I want to get as an answer to my query only ids that have more than 1 nums. For example in my table I would want to get as a result:

id
1

How should I express my query? Thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

Martín Lehoczky
Martín Lehoczky

Reputation: 98

You might need something like this:

SELECT id
FROM your_table_name
GROUP BY id
HAVING count(DISTINCT num) > 1;

Google 'Aggregate functions'. Here the aggregate function is count() and it works always coupled with a GROUP BY clause. Pretty fun.

Upvotes: 4

Related Questions