hakeの日記

Windows環境でプログラミングの勉強をしています。

SQLite - WHERE句

sqlite> select * from AAA;
ii          tt
----------  ----------
1           aaa
            bbb
3           ccc
4
5           ddd
6           eee
            fff
8
9           ggg

 

一致、比較、否定

対象カラムでNULL値のレコードは選択されない。

sqlite> select * from AAA where ii=3;
ii          tt
----------  ----------
3           ccc
sqlite> select * from AAA where tt='aaa';
ii          tt
----------  ----------
1           aaa
sqlite> select * from AAA where ii>4;
ii          tt
----------  ----------
5           ddd
6           eee
8
9           ggg
sqlite> select * from AAA where tt<'ccc';
ii          tt
----------  ----------
1           aaa
            bbb
sqlite> select * from AAA where tt<>'aaa';
ii          tt
----------  ----------
            bbb
3           ccc
5           ddd
6           eee
            fff
9           ggg
sqlite> select * from AAA where not tt<>'aaa';
ii          tt
----------  ----------
1           aaa

 

複合

対象カラムでNULL値のレコードは選択されない。

sqlite> select * from AAA where ii<>3 and tt<>'aaa';
ii          tt
----------  ----------
5           ddd
6           eee
9           ggg
sqlite> select * from AAA where ii=3 or tt='aaa';
ii          tt
----------  ----------
1           aaa
3           ccc

 

範囲

対象カラムでNULL値のレコードは選択されない。

sqlite> select * from AAA where ii between 3 and 5;
ii          tt
----------  ----------
3           ccc
4
5           ddd
sqlite> select * from AAA where tt between 'ccc' and 'ddd';
ii          tt
----------  ----------
3           ccc
5           ddd
sqlite> select * from AAA where not tt between 'bbb' and 'fff';
ii          tt
----------  ----------
1           aaa
9           ggg

 

NULLか否か

sqlite> select * from AAA where ii isnull;
ii          tt
----------  ----------
            bbb
            fff
sqlite> select * from AAA where ii not null;
ii          tt
----------  ----------
1           aaa
3           ccc
4
5           ddd
6           eee
8
9           ggg
sqlite> select * from AAA where ii notnull;
ii          tt
----------  ----------
1           aaa
3           ccc
4
5           ddd
6           eee
8
9           ggg

 

リストに含まれる/含まれない

sqlite> select * from AAA where ii in (1,2,3);
ii          tt
----------  ----------
1           aaa
3           ccc
sqlite> select * from AAA where ii not in (1,2,3);
ii          tt
----------  ----------
4
5           ddd
6           eee
8
9           ggg
sqlite> select * from AAA where ii in (select ii from AAA where tt='aaa');
ii          tt
----------  ----------
1           aaa
sqlite> select * from AAA where (ii,tt) in (select ii,tt from AAA where tt='aaa');
ii          tt
----------  ----------
1           aaa

 

パターンマッチ(LIKE)

注:日本語では上手くいかない場合がある

  • % :任意の0文字以上
  • _ :任意の1文字
  • カラム名 LIKE パターン文字 ESCAPE エスケープ文字(エスケープ文字に続く%や_は文字そのものとして扱う)
sqlite> select * from AAA where tt like '%a';
ii          tt
----------  ----------
1           aaa
sqlite> select * from AAA where tt like '__a';
ii          tt
----------  ----------
1           aaa
sqlite> select * from AAA where tt like '_@_B' escape '@';
                                --'@'の次の'_'は文字そのものを表す
                                --'_B'で終わる任意の3文字を示す

 

パターンマッチ(GLOB)

注:日本語では上手くいかない場合がある

  • * :任意の0文字以上
  • ? :任意の1文字
  • [abc] :a,b,cのいずれか
  • [a-d] :aからdのいずれか
  • *, %, [ , ] をエスケープする場合は[ ]で囲む