如何从表中获取所需的行

时间:2017-09-13 10:53:01

标签: mysql sql-server

我希望所有product_id都有website_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 |
+------------+------------+

任何人都可以帮我查询。谢谢!。

2 个答案:

答案 0 :(得分:7)

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

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

Demo

答案 1 :(得分:0)

select product_id,website_id
from table_name
group by product_id
having sum(website_id) < 1 

更多简化版。