cow12331
cow12331

Reputation: 245

MySQL query all outcome from a table where the field's values all exist in another table

For example, merchant(m_id), bill(b_id, m_id, payer_name) How can I get all payer who buy from every merchant? I used group by and compare the count, but the bill can occur times by same payer and same merchant. I think I need some function likewhere m_id in all (select m_id from merchant).

Upvotes: 0

Views: 35

Answers (1)

sgeddes
sgeddes

Reputation: 62841

There are a couple ways of doing this. Here's one using a subquery:

select payer_name
from bill 
group by payer_name
having count(distinct m_id) = (select count(*) from merchant)

Upvotes: 1

Related Questions