SQLite | データ検索 | WHERE句による条件指定

WHERE句を使うと、条件に一致する行だけを検索できる。

WHERE句による条件指定

SELECT column_name, ... FROM table_name WHERE condition;
create table user (name text, old integer, address text);
insert into user values ('devkuma', 39, 'Seoul');
insert into user values ('kimkc', 34, 'Busan');
insert into user values ('araikuma', 26, 'Seoul');
insert into user values ('happykuma', 19, 'Seoul');
insert into user values ('mykuma', 27, 'Daejeon');
insert into user values ('yourkuma', 28, 'Gwangju');
insert into user values ('raccoon', 31, 'Busan');
select * from user where address = 'Seoul';
select * from user where address = 'Busan';

最初の検索はSeoulの3行、次の検索はBusanの2行を返す。

比較演算子

演算子 意味
=== 等しい
<>!= 等しくない
>>= より大きい、以上
<<= より小さい、以下
select * from user where old >= 30;
select * from user where address <> 'Busan';

論理演算子

ANDは両方、ORはいずれか、NOTは条件を満たさない行を返す。

WHERE condition1 AND condition2
WHERE condition1 OR condition2
WHERE NOT condition
select * from user where old > 20 and address = 'Seoul';

最後のクエリはdevkumaaraikumaを返す。