讨论数量:
select * from order where product_id=318 and order_id not in (select order_id from order where product_id!=318 )
select * from order where product_id=318 and order_id in (select order_id from order where product_id=318 )
select * from order_detail group by order_id having count(if(product_id = 318,null ,1)) = 0
select * from order where not exists (select 1 from order_detail where order_detail.order_id = order.id and order_detail.product_id != 318)
select * from order_detail as od where not exists(select 1 from order_detail where order_detail.order_id = od.order_id and order_detail.product_id != 318)
SELECT
order_id,
GROUP_CONCAT(product_id) pids
FROM
(
SELECT
order_id,product_id
FROM
products p2
where
order_id IN (
SELECT
order_id
FROM
products p1
WHERE
product_id = 318)) p
group by
order_id
HAVING
pids = 318;
是想查询 只包含 318 一个产品的订单,不含其他品的订单是吧?
SELECT * form order WHERE order_id IN
(
select order_id from order
where order_id in (select order_id from order where product_id=318 )
group BY order_id
having COUNT(*) = 1
)
推荐文章: