<<insert/update/delete文



■基本


//テーブルの全カラム、全レコードを参照する
select * from tablename;


//カラムを指定する
select num1, str1 from tablename;


//distinct引数を使って重複させない
select distinct(num1) from tablename;


//where句を使った抽出条件指定
select num1, str1 from tablename where num1 > 5;


[比較演算子] =, >, <, >=, <=, <>


//where句を使った抽出条件指定(否定形)
select num1, str1 from tablename where not num1 > 5;


[論理演算子] not, and, or


//where句を使った抽出条件複数指定
select num1, str1 from tablename where num1 > 5 and str1='hoge';
select num1, str1 from tablename where num1 <> 5 or str1<>'hoge';


複数の条件は論理演算子and orで連結します。


//between演算子で抽出範囲指定
select num1, str1 from tablename where num1 between 3 and 8;


between A and Bでは、数値のみを指定できる。内部が数値表現の時刻なども可


//inキーワードを使った抽出
select num1, str1 from tablename where num1 in (1, 2, 3);


[リストを扱うキーワード] in, any, some, all, exists


//inキーワードを使った抽出(否定形)
select num1, str1 from tablename where num1 not in (1, 2, 3);


//inキーワードを使った副問い合わせ(サブクエリー)
select num1, str1 from tablename where num1 in (select distinct(num1) from tablename2);


//= anyはinキーワードと同格
select num1, str1 from tablename where num1 = any (select distinct(num1) from tablename2);


//副問い合わせの結果が確実に1レコードなら比較演算子を使える
select num1, str1 from tablename where num1=(select max(num1) from tablename2);


//like演算子を使った部分一致抽出(foo,fukuokaなどにマッチさせる例)
select num1, str1 from tablename where str1 like 'f%';


[ワイルドカード] 任意の1文字にマッチ「_」、任意の長さにマッチ「%」


//order by句を使って並び替え指定(昇順)
select num1, str1 from tablename order by num1;
select num1, str1 from tablename order by num1 asc;


ascは省略できる


//order by句を使って並び替え指定(降順)
select num1, str1 from tablename order by num1 desc;


//order by句で複数の並び替え条件指定
select num1, str1 from tablename order by num1,str1;


//null値を持つデータを抽出
select num1, str1 from tablename where str1 is null;




■集合関数


複数レコードを計算し、結果1レコードを返す


//sum()関数を使ったカラム値の合計
select sum(num1) from tablename;


//avg()関数を使ったカラム値の平均
select avg(num1) from tablename;


//max()関数を使ったカラム値の最大
select max(num1) from tablename;


//min()関数を使ったカラム値の最小
select min(num1) from tablename;


//count()関数を使ったレコード数の参照
select count(*) from tablename;


//count()とdistinct()関数を使ったユニークなレコード数の参照
select count(distinct(num1)) from tablename;




■グループ化


グループ分けの抽出条件には集合関数が多用される


//group by句を使ったグループ分け
select num1, count(*) from tablename group by num1;


//group by句に複数指定したグループ分け
select num1, count(*) from tablename group by num1, num2;


//having句を使ってグループ分けの抽出条件指定
select num1, count(*) from tablename group by num1 having count(*) > 5;


//グループ分けの並べ替え
select num1, count(*) from tablename group by num1 order by num1 asc;




■表結合


//テーブル結合
select tab1.num1, tab2.str1 from tab1,tab2 where tab1.num1 > 10 and tab1.num2=tab2.num2;


//自己結合(1テーブル内で、同じnum1値でかつstr1値の異なるものを抽出する例)
select a.num1, a.str1 from tablename a, tablename b where a.num1=b.num1 and a.str1<>b.str1;


from句のテーブル名 直後にエイリアス名を付ける


//INNER JOINで内部結合
select num1, num2, str1 from tab1 inner join tab2 on tab1.num1=tab2.num1 where num1 > 10;


内部結合の場合、両方の表に共通で存在するものだけが抽出される。


//LEFT JOINで外部結合(左側のすべての行表示)
select num1, num2, str1 from tab1 left join tab2 on tab1.num1=tab2.num1 where num1 > 10;


tab1すべての行を表示


//RIGHT JOINで外部結合(右側のすべての行表示)
select num1, num2, str1 from tab1 right join tab2 on tab1.num1=tab2.num1 where num1 > 10;


tab2すべての行を表示




(まだまだ補完途中です。)