アプリ版:「スタンプのみでお礼する」機能のリリースについて

以下の3つのテーブルから、下記のような結果を得たいです。

table1:PK=id
id(int), count(int)
-------------------
1, 11
2, 12
3, 13
5, 15

table2:PK=id
-------------------
id(int), count(int)
1, 21
2, 22
4, 24

tableID:PK=id
id(int)
-------------------
1
2
3
4
5
6


得たい結果
tableIDのすべてのIDに対して、
table2に見つかればtable2のID、
table2に見つからずtable1に見つかればtable1のID、
両方になければnull

id, count
----------
1, 21
2, 22
3, 13
4, 24
5, 15
6, null

よろしくお願いいたします。

A 回答 (1件)

一応結果は出たので参考程度に・・・



select a.id, coalesce(c.count, b.count, null)
from tableid as a
left join table1 as b
on(a.id = b.id)
left join table2 as c
on(a.id = c.id)
order by a.id


coalesce()
リスト内の最初の非NULL要素を返す。
    • good
    • 0
この回答へのお礼

coalesce()という関数が使えるのですね!
助かりました、どうもありがとうございます^^

お礼日時:2009/12/21 10:14

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

関連するカテゴリからQ&Aを探す