选择具有所有不同产品SQL的id_seller

时间:2018-11-19 13:20:12

标签: sql

我有以下表格:Table1Table2。我想要的输出是seller_nameid_seller,其中id_seller仅具有不同的product

表1:

+-----------+---------+
| id_seller | product |
+-----------+---------+
| 01        | 10      |
| 01        | 10      |
| 01        | 12      |
| 02        | 12      |
| 02        | 14      |
| 03        | 17      |
| 03        | 17      |
| 04        | 8       |
+-----------+---------+

表2:

+-----------+-------------+
| id_seller | seller_name |
+-----------+-------------+
| 01        | luigi       |
| 02        | mario       |
| 03        | bill        |
| 04        | simon       |
+-----------+-------------+

输出示例:

+-----------+-------------+
| id_seller | seller_name |
+-----------+-------------+
| 02        | mario       |
| 04        | simon       |
+-----------+-------------+

2 个答案:

答案 0 :(得分:0)

select t2.id_seller, t2.seller_name
from table2 t2
join table1 t1 on t1.id_seller = t2.id_seller
group by t2.id_seller, t2.seller_name
having count(t1.product) = count(distinct t1.product)

答案 1 :(得分:0)

您可以在下面尝试

select table1.id_seller, seller_name
from table1 left join table2 on table1.id_seller=table2.id_seller
group by table1.id_seller, seller_name
having count(product)=count(distinct product)