-1

Blanking out on how to get this data:

I have a table like this:

====================
ID     |    Source
====================
1      | google
1      | fb
1      | linkedIn
1      | linkedIn
3      | linkedIn
3      | fb

I want to return all the Ids that have 3 sources.

So in the above table, I want the result set to contain only id 1 since it was hit across 3 sources.

Database is mysql 5.7 so anything functions on 8.0 only won't work. Thanks for any help!

GMB
  • 216,147
  • 25
  • 84
  • 135
CD Brian
  • 43
  • 7
  • You have no primary key. Fix that, then see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query – Strawberry Aug 13 '20 at 16:54
  • Have you even tried anything??? – Eric Aug 13 '20 at 20:47

2 Answers2

2

Use aggregation:

select id 
from mytable 
group by id 
having count(distinct source) = 3
GMB
  • 216,147
  • 25
  • 84
  • 135
0

Try a query like this and see if that is helpful. It should return only IDs that have exactly 3 unique sources.

SELECT ytn.id, COUNT(DISTINCT ytn.source) AS unique_sources
FROM `your_table_name` ytn
GROUP BY ytn.id
HAVING COUNT(DISTINCT ytn.source) = 3;
Skully
  • 2,882
  • 3
  • 20
  • 31