LoginSignup
1
0

More than 1 year has passed since last update.

SQLメモ

Posted at

基本形

・テーブル内の情報を全て取得

select * from hogetable; 

・hogetableからnameカラムの値を全て取得

select name from hogetable;

・idが4の人の名前を取得する(文字列の場合は検索する値を「''」で囲む)

select name from hogetable where id = 4;

IN

複数検索する時
・nameカラムの値が大城,佐藤,田中のIDを取得する

select id from hogetable name IN('大城','佐藤','田中');

NOT IN

・nameカラムの値が大城,佐藤,田中以外のIDを取得する

select id from hogetable name NOT IN('大城','佐藤','田中');

LIKE

・名前に '大城' を含むレコードを抽出する。

select * from hogetable where name LIKE '大城%';

OR/XOR

・条件Aもしくは条件Bに合致しているやつ(OR)

select * from hogetable where 条件A OR 条件B;

・条件Aもしくは条件Bに合致しているやつで両方合致しているやつは省く(XOR)

select * from hogetable where 条件A XOR 条件B;

ROUND

・数値10.037を小数第2位で四捨五入する

select ROUND(10.037,2) from hogetable;

LENGTH

・国名 name と首都 capital が同じ長さの国の、国名と首都を表示する。

select name, capital from world where LENGTH (name) = LENGTH (capital);

SUBSTRING

・国名と首都の先頭の文字が同じである国の、国名と首都名を表示する。ただし、国名と首都名が同じ場合は除く

select name,capital from world 
where SUBSTRING(name, 1, 1) = SUBSTRING(capital,1,1)
and not name = capital;
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0