2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQL基礎

Last updated at Posted at 2021-01-16

SQL基礎の基礎 SELECT文から

(1)SELECT文

select カラム from テーブル;

大体これでOK

複数のカラムからデータを取得する場合は下のようにカンマで区切って追加する

select カラム,カラム2 from kakeibo;

(2)SELECT文WHEREを使う

select * from kakeibo where categoly = '食費'; 

データ型はクオーテーションマーク必要
カラム名categoryから食費の入ったレコードを取得する

where price = 1000;
数値の場合は、''クォーテーションマークをつけない
where purchased_at = '2010-10-09'
日付の場合もクォーテーションマークは必要

(3)比較演算子

大なり小なりの後にイコール=を入れる
>= 大なりイコール
<= 小なりイコール

where price >= 1000; `  例:1000以上 1000より大きい

上記は完全一致しか拾えません

(4)LIKE演算子

あいまい検索(ワイルドカード)
含むレコードを抽出できます

select * from purchases where name like '%プリン%';

前方一致
select * from purchases where name like 'プリン%'

後方一致
select * from purchases where name like '%プリン';

(5)not使い方 条件を満たさないデータ取得

where not name >= 1000;
where not name like '%プリン';

IS NULL
IS not NULL

(6)and演算子/or演算子

and演算子
where name = 'プリン' and price = 1000;

or演算子
where name is null or price is null;

where character_name = 'ねこ' or character_name = 'いぬ';

(7)データの並び替え

order by カラム名 並べ方;
asc 昇順に並び替える
desc 降順に並び替える

select *
from perchsed
where price = 1000
order by price desc;

(8)件数を限定する

何件を指定して取得する
My SQLやPostgreSQLでは使えますが、Oracleでは使えないんですね

limit データ件数;
limit 5;

(9)最大値・最小値を取得する

最大・最小値を調べたいとき
NULLがあっても、最大値を調べることができ、全部の列がNULLだった場合、NULLを返す

select MAX(カラム名) from テーブル名	
select MIN(カラム名) from テーブル名
2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?