1

テーブルを考える

results (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

お知らせ下さい。

4

1 に答える 1

4

1 つの方法は、GROUP BY と HAVING を使用して目的idの s を持つ派生テーブルを作成し、そこに JOIN することです。

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
)
于 2012-09-27T02:17:10.140 に答える