以下の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
よろしくお願いいたします。
No.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要素を返す。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・14歳の自分に衝撃の事実を告げてください
- ・架空の映画のネタバレレビュー
- ・「お昼の放送」の思い出
- ・昨日見た夢を教えて下さい
- ・ちょっと先の未来クイズ第4問
- ・【大喜利】【投稿~10/21(月)】買ったばかりの自転車を分解してひと言
- ・メモのコツを教えてください!
- ・CDの保有枚数を教えてください
- ・ホテルを選ぶとき、これだけは譲れない条件TOP3は?
- ・家・車以外で、人生で一番奮発した買い物
- ・人生最悪の忘れ物
- ・【コナン30周年】嘘でしょ!?と思った○○周年を教えて【ハルヒ20周年】
- ・ハマっている「お菓子」を教えて!
- ・最近、いつ泣きましたか?
- ・夏が終わったと感じる瞬間って、どんな時?
- ・10秒目をつむったら…
- ・人生のプチ美学を教えてください!!
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・都道府県穴埋めゲーム
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
エクセルで最後の文字だけ置き...
-
クエリ表示と、ADOで抽出したレ...
-
エクセルの関数について教えて...
-
Access パラメータクエリをcsv...
-
VIEWの元のテーブルのindexって...
-
バインド変数について
-
副問合せの書き方について
-
ある条件の最大値+1を初番する...
-
SQL Left Join で重複を排除す...
-
糸通しのあの人物っていったい誰?
-
selectした大量データをinsert...
-
複数JOINしているとCOUNTが正し...
-
エラー 1068 (42000): 複数の主...
-
SQLサーバから、項目の属性(型...
-
Mysql でレコードを追加した時...
-
SQL文のエラー
-
SQLにて特定の文字を除いた検索...
-
”photo id” とは何ぞや?
-
[MySQL] UNIQUE制約の値を更新...
-
mysqlについて
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
エクセルで最後の文字だけ置き...
-
エクセルの関数について教えて...
-
副問合せの書き方について
-
SQLサーバから、項目の属性(型...
-
VIEWの元のテーブルのindexって...
-
マイクラPC版のコマンドで効率...
-
SQL Left Join で重複を排除す...
-
エラー 1068 (42000): 複数の主...
-
select文のwhere句に配列を入れ...
-
バインド変数について
-
SQLにて特定の文字を除いた検索...
-
Access パラメータクエリをcsv...
-
WordpressのContact form 7でzi...
-
PL/SQLの変数について
-
1テーブル&複数レコードの更新...
-
ある条件の最大値+1を初番する...
-
Updateの複数テーブル条件時のL...
-
sqlで、600行あるテーブルを100...
-
”photo id” とは何ぞや?
-
クエリ表示と、ADOで抽出したレ...
おすすめ情報