Reputation: 19
I have a table with products, which I can order.
╔═════════╦═════════╗
║ ORDERID ║ INSTORE ║
╠═════════╬═════════╣
║ 1 ║ 0 ║
║ 1 ║ 1 ║
║ 1 ║ 1 ║
║ 1 ║ 1 ║
║ 1 ║ 0 ║
║ 2 ║ 1 ║
║ 2 ║ 1 ║
║ 2 ║ 1 ║
╚═════════╩═════════╝
Now I need to get all orders with instore all having value 1, Like orderid2. How is this possible?
Desired Result
╔═════════╗
║ ORDERID ║
╠═════════╣
║ 2 ║
╚═════════╝
Upvotes: 1
Views: 107
Reputation: 220
SELECT DISTINCT (
OrderID
)
FROM products
WHERE Instore =1
AND OrderID NOT
IN (
SELECT OrderID
FROM products
WHERE Instore
=0
)
Upvotes: 0
Reputation: 151
Not sure if I completely understand your question but
select * from products where instore=1 and orderid=2;
Upvotes: 0
Reputation: 19101
SELECT * FROM products p WHERE not exists (select 1 from products where orderid = p.orderid and instore != 1 limit 1)
Upvotes: 0
Reputation: 263693
SELECT orderID
FROM tableName
GROUP BY orderID
HAVING COUNT(*) = SUM(instore = 1)
Upvotes: 1