我會建議使用2聯接
SELECT *
FROM photo
JOIN photo_selectedTags as photo_selectedTags6 -- this join restricts to 'photo.COUNTER' whic have TAGS_COUNTER = 6
ON photo.COUNTER = photo_selectedTags6.PHOTO_COUNTER
AND photo_selectedTags6.TAGS_COUNTER = 6
JOIN photo_selectedTags as photo_selectedTags192 -- this join restricts to 'photo.COUNTER' whic have TAGS_COUNTER = 192
ON photo.COUNTER = photo_selectedTags192.PHOTO_COUNTER
AND photo_selectedTags192.TAGS_COUNTER = 192
也有可能實現它與一個alytical功能(如果你的數據庫支持)
-- This one works on teradata. Something similar should work on oracle. Don't know about others
SELECT *
FROM photo
LEFT JOIN photo_selectedTags
ON photo.COUNTER = photo_selectedTags.PHOTO_COUNTER
QUALIFY max(case when photo_selectedTags.TAGS_COUNTER = 6 then 1 end) over (partition by photo.COUNTER) = 1
AND max(case when photo_selectedTags.TAGS_COUNTER = 192 then 1 end) over (partition by photo.COUNTER) = 1
如果您在列表中(除192,6)有多個值,那麼這可能是可行的解決方案
SELECT *
FROM photo
JOIN
(
SELECT PHOTO_COUNTER, count(distinct TAGS_COUNTER) cnt
FROM photo_selectedTags
WHERE TAGS_COUNTER in (192,6)
HAVING cnt = 2 -- adjust this according to the number of different values
) as pht
ON photo.COUNTER = pht.PHOTO_COUNTER
在子查詢只PHOTO_COUNTERs剩下的都是(192和6),那麼這是加入
您是否試圖用IN替換ALL? – supertopi 2012-03-13 19:42:24
我個人會這樣做'WHERE photo_selectedTags.TAGS_COUNTER = 6 AND photo_selectedTags.TAGS_COUNTER = 192' – ScottJShea 2012-03-13 19:44:11
當我使用AND子句時,它不會返回任何行,即使我是積極的存在。我認爲存在的問題是,有一個照片行,但2 photo_selectedTags單行(一個是6,一個是192) – 2012-03-13 19:47:46