mysql 有且只有的sql怎么写

如图所示,这个是订单的子表,我想查询包含 product_id = 318 并且只含有318 的数据,这个应该怎么写呀
mysql  有且只有的sql怎么写

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 5
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 )
1年前 评论
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)
1年前 评论
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;
1年前 评论

是想查询 只包含 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
)
1年前 评论

这样?


select order_id, count(distinct product_id) n from t1 where order_id in (select order_id from t1 where product_id=318) group by order_id having n=1
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!