1
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 5 years have passed since last update.

【SQL】メモ5(ある順番で表示(ORDER BY)、結合(INNER JOIN))、結合(UNION)

Last updated at Posted at 2020-02-01

目的

SQL忘却防止

メモ1で使用しているテーブルを引用。

1.ある項目の順番で表示(SELECT ORDER BY)

形式:
SELECT 項目1,,,項目N FROM テーブル名 ORDER BY 項目1 表示順,,項目K 表示順

-- type0を基準に並べ替える
SELECT * FROM tbl_dino
 ORDER BY type0;

実行結果

id dname0 type0 n_src1 type1 n_src2 type2
6 ティラノサウルス epic 0 0
9 ノドパトサウルス epic 4 rare 3 normal
12 ケントロサウルス epic 0 0
14 ステゴケラトプス epic 13 rare 2 normal
17 エルリコサウルス epic 0 0
23 アンキロサウルス epic 0 0
1 ベロキラプトル normal 0 0
2 ステゴサウルス normal 0 0
3 アパトサウルス normal 0 0
10 ドラコレックス normal 0 0
11 アンキロサウルス normal 0 0
15 トリケラトプス normal 0 0
18 イリタトル normal 0 0
19 スコミムス normal 0 0
4 ノドサウルス rare 0 0
7 ユタラプトル rare 0 0
13 トリケラトプス rare 0 0
20 スコタトル rare 19 normal 18 normal
8 インドミナスレックス regend 6 epic 1 normal
16 ドラコケラトプス regend 15 normal 10 normal
21 ステゴディアス regend 9 epic 2 normal
24 アンキトロサウルス regend 23 epic 12 epic

1-2.準備

-- 非ハイブリッド恐竜を定義
CREATE TEMPORARY TABLE tmp_nohibrid as
 SELECT T1.id,T1.type0,T1.dname0
 FROM tbl_dino T1
 WHERE T1.n_src1 = 0 AND T1.n_src2=0;

表示

id type0 dname0
1 normal ベロキラプトル
2 normal ステゴサウルス
3 normal アパトサウルス
4 rare ノドサウルス
6 epic ティラノサウルス
7 rare ユタラプトル
10 normal ドラコレックス
11 normal アンキロサウルス
12 epic ケントロサウルス
13 rare トリケラトプス
15 normal トリケラトプス
17 epic エルリコサウルス
18 normal イリタトル
19 normal スコミムス
23 epic アンキロサウルス

2.テーブルの内部結合(INNER JOIN)

SELECT T1.id
,T1.dname0
,T1.type0
,T2.dname0 as dname1
,T1.type1
,T3.dname0 as dname2
,T1.type2
 FROM tbl_dino T1
 INNER JOIN tmp_nohibrid T2
 on T1.n_src1=T2.id
 INNER JOIN tmp_nohibrid T3
 on T1.n_src2=T3.id;

実行結果

生成元1と生成元2の恐竜名が表示。
数字で管理されている項目をユーザーにわかりやすく見せるために、
よく使うタイプのSQLかもしれない。

id dname0 type0 dname1 type1 dname2 type2
8 インドミナスレックス regend ティラノサウルス epic ベロキラプトル normal
9 ノドパトサウルス epic ノドサウルス rare アパトサウルス normal
14 ステゴケラトプス epic トリケラトプス rare ステゴサウルス normal
16 ドラコケラトプス regend トリケラトプス normal ドラコレックス normal
20 スコタトル rare スコミムス normal イリタトル normal
24 アンキトロサウルス regend アンキロサウルス epic ケントロサウルス epic

UNION(結合)

===>特徴は、2つのselect結果がお互いの情報を含まない集合であること

例:epicという種類を含む恐竜の種類を表示
1.非ハイブリッド種のepic
2.ハイブリッド種で、生成元がepic
この2種類のSQLを結合

SELECT * FROM tbl_dino
 WHERE type0='epic' AND n_src1=0 AND n_src2=0
 UNION
 SELECT * FROM tbl_dino
 WHERE type1='epic' or type2='epic'
 ORDER by n_src1,n_src2;

実行結果

id dname0 type0 dname1 type1 dname2 type2
6 ティラノサウルス epic 0 0
12 ケントロサウルス epic 0 0
17 エルリコサウルス epic 0 0
23 アンキロサウルス epic 0 0
8 インドミナスレックス regend 6 epic 1 normal
21 ステゴディアス regend 9 epic 2 normal
24 アンキトロサウルス regend 23 epic 12 epic
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?