考虑一张表
结果(id,key,value),其中key是主键。
示例数据:
id | key | value
-----------------------
abc| source-1 | 20
abc| source-2 | 30
abc| source-3 | 2
abc| source-4 | 10
def| source-5 | 1
ghi| source-6 | 25
jkl| source-5 | 13我只想返回那些具有给定id的单个条目的记录。所以输出应该是
id | key | value
------------------------
def| source-5 | 1
ghi| source-6 | 25
jkl| source-5 | 13请给我建议。
发布于 2012-09-27 10:17:10
一种方法是使用GROUP BY,并且必须生成具有所需id的派生表,然后连接到该表:
select results.*
from results
join (
select id
from results
group by id
having count(*) = 1
) as dt on results.id = dt.id如果您不喜欢派生表,也可以使用IN:
select *
from results
where id in (
select id
from results
group by id
having count(*) = 1
)https://stackoverflow.com/questions/12613062
复制相似问题