我希望所有产品的id都有网站id=0(零),并且没有其他值。下面是我的表格数据,

+------------+------------+
| product_id | website_id |
+------------+------------+
|      19112 |          0 |
|      19112 |          1 |
|      19112 |          2 |
|      19113 |          1 |
|      19110 |          0 |
|      19110 |          2 |
|      19114 |          0 |
|      19115 |          0 |
|      19115 |          1 |
|      19116 |          0 |
+------------+------------+

我想要上表数据的以下行;
+------------+------------+
| product_id | website_id |
+------------+------------+
|      19114 |          0 |
|      19116 |          0 |
+------------+------------+

有人能帮我查询一下吗。谢谢您!.

最佳答案

要获得所需的结果,可以使用以下查询

select product_id,website_id
from t
group by product_id
having count(*)=sum(website_id = 0)

Demo

10-06 12:30