gpt4 book ai didi

MySQL,胶表计数

转载 作者:行者123 更新时间:2023-11-29 06:58:04 24 4
gpt4 key购买 nike

在粘合表 product_options 中,我有以下字段...id、productid、optionid

CREATE TABLE `product_options` (
`id` int NULL AUTO_INCREMENT ,
`productid` int NULL ,
`optionid` int NULL ,
PRIMARY KEY (`id`)
)
;

INSERT INTO product_options
(productid, optionid)
VALUES
(1,2),(1,4),(1,5),(1,6),(1,7),(2,4),(2,3),(2,1),(2,7),(3,1),(3,4),(4,1),(4,7),(4,6),(5,1)

(参见 http://www.sqlfiddle.com/#!2/3d309)

现在,我想计算不同选项组合的产品数量。

例如,我想要的结果是...
选项 ID 为 6 和 7 的产品数量 = 2
所有选项 ID 为 6、7 和 1 的产品数量 = 1
选项 ID 为 1 = 4 的产品数量

我脑子卡住了,想不通 - 请帮忙...

最佳答案

select productid from product_options
where optionid in (6,7)
group by productid
having count(distinct optionid)=2

关于MySQL,胶表计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11556260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com