我有以下图片数据库(图片可以有多个功能):
Table Picture
-------------
ID Name
1 Mona Lisa
2 Scream
Table Features
-------------
ID Name PictureID (FK to Picture)
1 Portrait 2
2 Expressive 2
3 Big 2
4 Small 1
5 Expressive 1
5 Big 1
我希望做一个查询,检索所有肖像和大图片(因此结果将是这种情况下" Scream")。我已经提出了这个查询来检索它,但我不确定它是否是最漂亮和最有效的方法。这是我的建议:
SELECT *
FROM Picture o
WHERE
(select count(*)
from Feature c
where o.id = c.pictureID and c.Name like '%Portrait%') >= 1
AND
(select count(*)
from Feature c
where o.id = c.pictureID and c.Name like '%Big%') >= 1
在这种情况下,我必须两次浏览功能表(根据我的个人喜好,我发现它"丑陋")。
谢谢和问候
答案 0 :(得分:1)
SELECT picture.name FROM picture
JOIN features
on picture.id=features.pictureID
WHERE features.name IN('Portrait','Big')
GROUP BY features.pictureID
HAVING COUNT(DISTINCT features.ID)>=2