やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

SQLite3構文 expression(サブクエリ)

 ()内にあるselect文。

成果物

情報源

一覧

サブクエリ

スカラー・サブクエリ

select * from (select 1);

行値・サブクエリ

select * from (select 1 union select 2);
1
2
select (2,'B') = (select 1 as A, 'A' as B union select 2,'B');
0
select (2,'B') in (select 1 as A, 'A' as B union select 2,'B');
1

相関・サブクエリ

 サブクエリ内で外側クエリのテーブルを使う。

create table T(A integer primary key);
create table U(A integer references T(A));
insert into T values(0);
insert into T values(1);
insert into U values(0);
select U.A from U where U.A = (select A from T where T.A = U.A);
0

 サブクエリ内でテーブルUを使っているのがポイント。これは外側クエリのテーブルである。

 ちなみに、ふつうはJOINしたほうが高速らしい。

select U.A from U left join T on T.A = U.A;
0

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1218 SMP Tue May 14 00:48:17 BST 2019 armv7l GNU/Linux

前回まで