LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

SQL 第2版 ゼロからはじめるデータベース操作2章

Last updated at Posted at 2020-06-10

検索の基本

select区(データの出力)

全データの出力

select * from shohin; --shohinテーブルから*(全列)を選ぶ
shohin_id |  shohin_mei   | shohin_bunrui | hanbai_tanka | shiire_tanka |  torokubi  
-----------+-----------------+---------------+--------------+--------------+------------
 0003         | カッターシャツ  | 衣服          |         4000 |         2800 | 
 0001         | Tシャツ       | 衣服          |         1000 |          500 | 2009-09-20
 0002         | 穴あけパンチ    | 事務用品      |          500 |          320 | 2009-09-11
 0004         | 包丁          | キッチン用品  |         3000 |         2800 | 2009-09-20
 0005         | 圧力鍋        | キッチン用品  |         6800 |         5000 | 2009-01-15
 0006         | フォーク        | キッチン用品  |          500 |              | 2009-09-20
 0007         | おろしがね       | キッチン用品  |          880 |          790 | 2009-04-28
 0008         | ボールペン       | 事務用品      |          100 |              | 2009-11-11
(8 rows)

カラムに名前をつけて出力する

select shohin_id as "ID",shohin_mei as 商品名 from shohin;
ID  |     商品名                                                    
------+------------- 
 0001    | Tシャツ                                                                                                
 0002    | 穴あけパンチ                                                                                              
 0003    | カッターシャツ                                                                                             
 0004    | 包丁                                                                                                  
 0005    | 圧力鍋                                                                                                 
 0006    | フォーク                                                                                                
 0007    | おろしがね                                                                                               
 0008    | ボールペン                                                                                               
(8 rows)

定数の出力

select '商品' as mojiretsu, 38 as kazu, '2020-01-01' as hizuke, shohin_id, shohin_mei
from shohin;
 mojiretsu | kazu |   hizuke   | shohin_id |     shohin_mei                                                  
-----------+------+------------+-----------+-----------------------------
 商品      |   38 | 2020-01-01 | 1         | Tシャツ                                                                                                
 商品      |   38 | 2020-01-01 | 2         | 穴あけパンチ                                                                                              
 商品      |   38 | 2020-01-01 | 3         | カッターシャツ                                                                                             
 商品      |   38 | 2020-01-01 | 4         | 包丁                                                                                                  
 商品      |   38 | 2020-01-01 | 5         | 圧力鍋                                                                                                 
 商品      |   38 | 2020-01-01 | 6         | フォーク                                                                                                
 商品      |   38 | 2020-01-01 | 7         | おろしがね                                                                                               
 商品      |   38 | 2020-01-01 | 8         | ボールペン                                                                                               
(8 rows)

重複したデータをまとめる

select distinct shohin_bunrui from shohin;
shohin_bunrui 
---------------
 キッチン用品
 衣服
 事務用品
(3 rows)

複数の列でDISTINCTを用いた場合

select distinct shohin_bunrui, torokubi
from shohin;
 shohin_bunrui |  torokubi  
---------------+------------
 衣服          | 
 キッチン用品  | 2009-01-15
 キッチン用品  | 2009-04-28
 衣服          | 2009-09-20
 事務用品      | 2009-11-11
 事務用品      | 2009-09-11
 キッチン用品  | 2009-09-20

where句(レコードを条件で選択)

 select shohin_mei, shohin_bunrui
 from shohin
 where shohin_bunrui = '衣服'; --分類が衣服のものを出力
  shohin_mei  | shohin_bunrui 
--------------+---------------
 カッターシャツ   | 衣服
 Tシャツ        | 衣服
(2 rows)

演算子

算術演算子

select shohin_mei, hanbai_tanka,hanbai_tanka * 2 as hanbai_tanka_x2 from shohin; --他にも+,-,/
 shohin_mei  | hanbai_tanka | hanbai_tanka_x2 
-------------+--------------+-----------------
 カッターシャツ  |         4000 |            8000
 Tシャツ       |         1000 |            2000
 穴あけパンチ   |          500 |            1000
 包丁         |         3000 |            6000
 圧力鍋        |         6800 |           13600
 フォーク        |          500 |            1000
 おろしがね      |          880 |            1760
 ボールペン      |          100 |             200
(8 rows)

select (1 + 3 - 1)*100/10 as kotae;
 kotae 
-------
    30

比較演算子

・ = 〜と等しい
・ <> ~と等しくない
・ >= 〜以上
・ > 〜より大きい
・ <= 〜以下
・ < ~より小さい

select shohin_mei, shohin_bunrui, hanbai_tanka
from shohin
where hanbai_tanka <> 1000; --1000円でないものを出力
shohin_mei   | shohin_bunrui | hanbai_tanka 
------------+---------------+--------------
 カッターシャツ  | 衣服          |         4000
 穴あけパンチ  | 事務用品      |          500
 包丁         | キッチン用品  |         3000
 圧力鍋       | キッチン用品  |         6800
 フォーク       | キッチン用品  |          500
 おろしがね     | キッチン用品  |          880
 ボールペン     | 事務用品      |          100
(7 rows)

WHERE区の条件式に計算式を書く

select shohin_mei, hanbai_tanka, shiire_tanka
from shohin
where hanbai_tanka - shiire_tanka >= 500;
shohin_mei | hanbai_tanka | shiire_tanka 
------------+--------------+--------------
 カッターシャツ |         4000 |         2800
 Tシャツ      |         1000 |          500
 圧力鍋       |         6800 |         5000
(3 rows)

文字列の不等号に注意

データの用意

create table chars
(chr char(3) not null,
 primary key (chr));
begin transaction;
insert into chars values ('1'),('2'),('3'),('10'),('11'),('222');
commit;

select chr
from chars
where chr > '2' ; --2より大きいのだから,'10','11'もヒットするはず
 chr 
-----
 3  
 222
(2 rows)

とうことで,'10','11'もヒットしない。
理由は文字列だから。
文字列は先頭の文字で比較するので、文字列を昇順で並び替えると
1
10
11
2
222
3
という順番となり、10と11はヒットしない。

NULLに比較演算子は使えない

select * from shohin where shiire_tanka = null;
 shohin_id | shohin_mei | shohin_bunrui | hanbai_tanka | shiire_tanka | torokubi 
-----------+------------+---------------+--------------+--------------+----------
(0 rows)

NULLでないも以下のようになる

select * from shohin where shiire_tanka <> null;
 shohin_id | shohin_mei | shohin_bunrui | hanbai_tanka | shiire_tanka | torokubi 
-----------+------------+---------------+--------------+--------------+----------
(0 rows)

なのでNULLを呼び出すときはIS NULLを使う

select * from shohin where shiire_tanka  is null;
 shohin_id | shohin_mei | shohin_bunrui | hanbai_tanka | shiire_tanka |  torokubi  
-----------+------------+---------------+--------------+--------------+------------
 0006      | フォーク   | キッチン用品  |          500 |              | 2009-09-20
 0008      | ボールペン | 事務用品      |          100 |              | 2009-11-11
(2 rows)

逆にNULLじゃないもをを呼び出すときはIS NOT NULLを使う

select * from shohin where shiire_tanka  is not null;
 shohin_id |   shohin_mei   | shohin_bunrui | hanbai_tanka | shiire_tanka |  torokubi  
-----------+----------------+---------------+--------------+--------------+------------
 0001      | Tシャツ        | 衣服          |         1000 |          500 | 2009-09-20
 0002      | 穴あけパンチ   | 事務用品      |          500 |          320 | 2009-09-11
 0003      | カッターシャツ | 衣服          |         4000 |         2800 | 
 0004      | 包丁           | キッチン用品  |         3000 |         2800 | 2009-09-20
 0005      | 圧力鍋         | キッチン用品  |         6800 |         5000 | 2009-01-15
 0007      | おろしがね     | キッチン用品  |          880 |          790 | 2008-04-28
(6 rows)

他にもNOT演算子の使い方がある

select * from shohin where not hanbai_tanka >= 1000;
 shohin_id |  shohin_mei  | shohin_bunrui | hanbai_tanka | shiire_tanka |  torokubi  
-----------+--------------+---------------+--------------+--------------+------------
 0002      | 穴あけパンチ | 事務用品      |          500 |          320 | 2009-09-11
 0006      | フォーク     | キッチン用品  |          500 |              | 2009-09-20
 0007      | おろしがね   | キッチン用品  |          880 |          790 | 2008-04-28
 0008      | ボールペン   | 事務用品      |          100 |              | 2009-11-11
(4 rows)

1000円以上でない。つまり1000円未満ということになるが分かりにくくなる。
なので無理に使う必要はない。

AND演算子とOR演算子

select * from shohin where shohin_bunrui = 'キッチン用品' and hanbai_tanka >=3000; --分類がキッチン用品かつ販売単価が3000円以上という条件
 shohin_id | shohin_mei | shohin_bunrui | hanbai_tanka | shiire_tanka |  torokubi  
-----------+------------+---------------+--------------+--------------+------------
 0004      | 包丁       | キッチン用品  |         3000 |         2800 | 2009-09-20
 0005      | 圧力鍋     | キッチン用品  |         6800 |         5000 | 2009-01-15
(2 rows)

select * from shohin where shohin_bunrui = 'キッチン用品' or hanbai_tanka >=3000; --分類がキッチン用品あるいは販売単価が3000円以上という条件
 shohin_id | shohin_mei| shohin_bunrui | hanbai_tanka | shiire_tanka |  torokubi  
-----------+------------+---------------+--------------+--------------+------------
 0003         | カッターシャツ | 衣服          |         4000 |         2800 | 
 0004         | 包丁        | キッチン用品  |         3000 |         2800 | 2009-09-20
 0005         | 圧力鍋       | キッチン用品  |         6800 |         5000 | 2009-01-15
 0006         | フォーク      | キッチン用品  |          500 |              | 2009-09-20
 0007         | おろしがね    | キッチン用品  |          880 |          790 | 2009-04-28
(5 rows)

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