Reputation: 36674
I have a Products
table
with ProductId
and StatusId
columns
How can i select all the products which have StatusId
1 and 2 (two rows) ?
Upvotes: 2
Views: 7977
Reputation: 8563
Try this query to get records for product having both the status 1 and 2
SELECT DISTINCT a.ProductId
FROM Products a, Products b
WHERE a.ProductId = b.ProductId
AND a.StatusId = 1
AND b.StatusId = 2;
USING ANSI JOIN
SELECT DISTINCT a.ProductId
FROM Products a INNER JOIN Products b
ON a.ProductId = b.ProductId
WHERE a.StatusId = 1
AND b.StatusId = 2;
Upvotes: 2
Reputation: 33849
To select Products with both status (1,2):
SELECT ProductId
FROM products
WHERE status IN (1, 2)
GROUP BY ProductId
HAVING COUNT(distinct status) = 2
Upvotes: 7
Reputation: 66727
SELECT DISTINCT a.ProductId
FROM Products a
INNER JOIN Products b ON a.ProductId = b.ProductId
WHERE a.StatusId IN (1, 2)
Upvotes: 0