gpt4 book ai didi

Mysql + where子句匹配多行

转载 作者:行者123 更新时间:2023-11-29 01:27:19 24 4
gpt4 key购买 nike

我有一个这样的表:

id  image_id  style_id
------------------------
1 45 24
1 45 25
1 47 25
1 45 27
1 45 28

我想提取以下所有三个条件都匹配的 image_id 列:

style_id = 24
style_id = 25
style_id = 27

我有这样的查询:

SELECT image_id FROM list WHERE (style_id = 24 AND style_id = 25 AND style_id = 27)

它没有返回 image_id 45。

最佳答案

试试这个:

SELECT image_id 
FROM list
WHERE style_id IN (24, 25, 27)
GROUP BY image_id
HAVING COUNT(DISTINCT style_id) = 3

DISTINCT 关键字仅在每个 image_idstyle_id 字段有重复值的情况下才是必需的。

关于Mysql + where子句匹配多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36014390/

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