データを抽出する
全てのデータを抽出する
select * from users;
カラムを指定して抽出する
select id, name from users;
条件をつける
< > <= >= = <> !=
is null, is not null
and or not
float型の様な小数についてはうまく表すことができず多くのデータベースは小数を「それに最も近い数値」として保存するようになっています。
数値を整数に変換してから保存する。
scoreが6.0以上
select * from users where score >= 6.0;
score が3.0以上6.0以下
select * from users where score >= 3.0 and score <= 6.0;
select * from users where score between 3.0 and 6.0;
name が AAA か BBB
select * from users where name = 'taguchi' or name = 'fkoji';
select * from users where name in ('taguchi', 'fkoji');
文字列が一致
select * from users where name = 'taguchi';
特定の1文字から始まる like '文字%'
select * from users where name like 't%';
特定の1文字で終わる like '%文字'
select * from users where name like '%a';
特定の1文字を含む like '%文字%'
select * from users where name like '%a%';
大文字と小文字を区別する ike binary '文字%';
select * from users where name like binary 'T%';
文字数を指定する like '______';
6文字
select * from users where name like '______';
何文字目に特定の文字を含む like '__文字%';
2文字目にaを含む
+select * from users where name like '_a%';
並び替え、抽出件数の制限
小さい順に並び替える。 select * from テーブル名 order by カラム名;
select * from users order by score;
大きい順に並び替える。 select * from テーブル名 order by カラム名 desc;
select * from users where score is not null order by score desc;
件数を制限する limit 件数;
select * from users limit 3;
次の 数件を表示したい limit 飛ばす件数 offset 表示する件数;
select * from users limit 3 offset 3;
組み合わせると
select * from users order by score desc limit 3;