1

我有一张桌子,上面有我可以订购的产品。

╔═════════╦═════════╗
║ ORDERID ║ INSTORE ║
╠═════════╬═════════╣
║       1 ║       0 ║
║       1 ║       1 ║
║       1 ║       1 ║
║       1 ║       1 ║
║       1 ║       0 ║
║       2 ║       1 ║
║       2 ║       1 ║
║       2 ║       1 ║
╚═════════╩═════════╝

现在我需要获取所有 instore 的订单,所有订单的值都为 1,比如 orderid2。这怎么可能?

期望的结果

╔═════════╗
║ ORDERID ║
╠═════════╣
║       2 ║
╚═════════╝
4

4 回答 4

1
SELECT  orderID
FROM    tableName
GROUP   BY orderID
HAVING  COUNT(*) = SUM(instore = 1)
于 2013-02-01T15:37:05.197 回答
0
SELECT * FROM products p WHERE not exists (select 1 from products where orderid = p.orderid and instore != 1 limit 1)
于 2013-02-01T15:37:58.707 回答
0

不确定我是否完全理解你的问题,但

select * from products where instore=1 and orderid=2;
于 2013-02-01T15:39:25.113 回答
0
SELECT DISTINCT (

OrderID ) FROM products WHERE Instore =1 且 OrderID 不在 (

SELECT OrderID FROM products WHERE Instore=0 )

于 2013-02-05T13:34:03.973 回答