MySQL + where子句匹配多行 [英] Mysql + where clause matching multiple rows

查看:216
本文介绍了MySQL + where子句匹配多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的表:

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

我想提取满足以下所有三个条件的image_id列:

I want to pull image_id column where all three below conditions match:

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.

which doesn't return me 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

仅当您每个image_id可以具有style_id字段的重复值时,才需要DISTINCT关键字.

The DISTINCT keyword is only necessary in case you can have duplicate values of style_id field per image_id.

这篇关于MySQL + where子句匹配多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆