我有这个SQL表:
name | id_photo
================
word1 | 38
word2 | 38
word7 | 39
word1 | 31
word2 | 31
我想研究所有id_photo,例如name = word1 AND name = word2 例如,这种情况我想作为回报38,31
有人可以提出解决方案吗?
答案 0 :(得分:2)
SELECT ID_PHOTO
FROM tableNAME
WHERE name IN ('word1','word2')
GROUP BY ID_PHOTO
HAVING COUNT(*) = 2
如果未在每个id_photo
的名称上指定唯一键,请使用distinct
内的COUNT
SELECT ID_PHOTO
FROM tableNAME
WHERE name IN ('word1','word2')
GROUP BY ID_PHOTO
HAVING COUNT(DISTINCT name) = 2