SQL结果来自不同行的研究

时间:2012-11-16 14:39:07

标签: php mysql sql

我有这个SQL表:

name  | id_photo
================
word1 | 38    
word2 | 38
word7 | 39
word1 | 31
word2 | 31

我想研究所有id_photo,例如name = word1 AND name = word2 例如,这种情况我想作为回报38,31

有人可以提出解决方案吗?

1 个答案:

答案 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