Mahendra Jella
Mahendra Jella

Reputation: 5596

How to get the required rows from a table

I want all product_id's having website_id = 0(Zero) and not having other value. Following is my table data,

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

I want following rows from above table data;

+------------+------------+
| product_id | website_id |
+------------+------------+
|      19114 |          0 |
|      19116 |          0 |
+------------+------------+

Can any one help me with query. Thank you!.

Upvotes: 1

Views: 54

Answers (2)

Imran Ali
Imran Ali

Reputation: 63

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

More Simplified version.

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64486

To get the desired result you can use following query

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

Demo

Upvotes: 7

Related Questions