共通表式 WITH

前提

 SELECT * FROM t1;
  id1 | id2
 -----+-----
    1 |   2
    3 |   4
    5 |   6
 (3 rows)
 SELECT * FROM t2;
  id
 ----
   1
   3
 (2 rows)

WITHを使わない

 SELECT * FROM t1 WHERE id1 IN (SELECT id FROM t2 WHERE id > 1) OR id2 IN (SELECT id FROM t2 WHERE id > 1);
  id1 | id2
 -----+-----
    1 |   2
    3 |   4
 (2 rows)

WITHを使う

  WITH _t2 AS (SELECT id FROM t2 WHERE id > 1) 
  SELECT * FROM t1 WHERE id1 IN (SELECT id FROM _t2) OR id2 IN (SELECT id FROM _t2);
  id1 | id2
 -----+-----
    3 |   4
 (1 row)

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS