0

我有这个 SQL 表:

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

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

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

4

1 回答 1

2
SELECT ID_PHOTO
FROM tableNAME
WHERE name IN ('word1','word2')
GROUP BY ID_PHOTO
HAVING COUNT(*) = 2

如果没有在每个名称上指定唯一键,请在内部id_photo使用distinctCOUNT

SELECT ID_PHOTO
FROM tableNAME
WHERE name IN ('word1','word2')
GROUP BY ID_PHOTO
HAVING COUNT(DISTINCT name) = 2
于 2012-11-16T14:40:45.727 回答