LoginSignup
0
1

More than 5 years have passed since last update.

SQL_4大命令_基礎

Last updated at Posted at 2018-02-03

最近勉強したクエリのメモ(・∀・)
この4つが分かれば簡単な調査とか更新が可能!

SQL 4大命令

・ select(検索)
・ update(変更)
・ delete(削除)
・ insert(追加)

※このメモには書いてないが、update / delete / insert を実際に更新する際は、トランザクションをつけたほうが良い。

使用例 select

■ 全てのカラムを抽出

select * from テーブル名

■ 抽出するカラムを指定

select カラムA, カラムB from テーブル名

■ 抽出する条件を指定

select * from テーブル名
where カラムC = '日本食'

■ 抽出する条件を複数指定

select * from テーブル名
where カラムC = '日本食' and カラムE >= '2018-02-05'

■ 抽出する条件を〜から〜までを指定

select * from テーブル名
where カラムE between '2018-02-05' and '2018-02-08'

■ 抽出する条件を()内のどれかに一致する場合の指定

select * from テーブル名
where カラムC in ('日本食', '洋食') 

■ 抽出する条件を()内のどれにも一致するしない場合の指定

select * from テーブル名
where カラムC not in ('日本食', '洋食') 

■抽出する条件を曖昧に指定 (後方一致var)

select * from テーブル名 
where カラムC like '%食' 

■抽出する条件を曖昧に指定 (前方一致var)

select * from テーブル名 
where カラムC like '日本%' 

■抽出する条件を曖昧に指定 (前方後方一致var)

select * from テーブル名 
where カラムC like '%本%' 

■ 抽出する条件をNULLに指定(nullの抽出は特殊)

select * from テーブル名
where カラムB is null

■ 抽出する条件をNULLじゃないデータを指定(nullの抽出は特殊)

select * from テーブル名
where カラムB is not null

使用例 update

■ 全てのカラムAを更新

update テーブル名
set カラムA = カラムA + 000

■ 更新する条件を指定して更新

update テーブル名
set カラムA = カラムA + 000
where カラムB = A01

使用例 delete

■テーブル内全てのデータを削除

delete from テーブル名

■削除する条件を指定

delete from テーブル名
where カラムC = '日本食'

使用例 insert

■追加するカラムを指定(2行目で指定したカラム順に、3行目データを入れる)

insert into テーブル名
(ID, code, category)
values (12345, A05, '洋食')

■カラムを指定せずに追加

insert into テーブル名
values (12345, A05, '洋食','肉料理','2018-0205')

以上!

0
1
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
0
1