Boz
Boz

Reputation: 19

MYSQL/PHP Select same values

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

Answers (4)

Kannan Rajendran
Kannan Rajendran

Reputation: 220

SELECT DISTINCT (

OrderID ) FROM products WHERE Instore =1 AND OrderID NOT IN (

SELECT OrderID FROM products WHERE Instore =0 )

Upvotes: 0

alxkls
alxkls

Reputation: 151

Not sure if I completely understand your question but

select * from products where instore=1 and orderid=2;

Upvotes: 0

palindrom
palindrom

Reputation: 19101

SELECT * FROM products p WHERE not exists (select 1 from products where orderid = p.orderid and instore != 1 limit 1)

Upvotes: 0

John Woo
John Woo

Reputation: 263693

SELECT  orderID
FROM    tableName
GROUP   BY orderID
HAVING  COUNT(*) = SUM(instore = 1)

Upvotes: 1

Related Questions