0
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 3 years have passed since last update.

メモ④-カラム名の変更とテーブルが異なるカラム同士の結合-

Last updated at Posted at 2020-02-28

MariaDBでは、表の項目名を自由に設定し表示することができる。

【カラム名を変更しての表示】
例えば、以下のような表示。
MariaDB [company]> SELECT
-> emp.code AS emp_code,
-> emp.name AS emp_name,
-> post.code AS post_code,
-> post.name AS post_name
-> FROM
-> tbl_employee AS emp
-> LEFT OUTER JOIN tbl_post AS post
-> ON emp.post_code = post.code
-> ORDER BY
-> emp.code
-> ;
+----------+----------+-----------+-----------+
| emp_code | emp_name | post_code | post_name |
+----------+----------+-----------+-----------+
| 101 | 伊藤英樹 | 1 | 部長 |
| 102 | 山本大樹 | 3 | 課長 |
| 103 | 中村千佳 | 4 | 係長 |
| 104 | 小林谷雄 | NULL | NULL |
| 105 | 斎藤美緒 | NULL | NULL |
+----------+----------+-----------+-----------+
5 rows in set (0.000 sec)

このように書き換えることで、
カラム名を変更して表示することができる。

MariaDB [company]> SELECT
-> emp.code AS "社員コード",
-> emp.name AS "社員名",
-> post.code AS "役職コード",
-> post.name AS "役職名"
-> FROM
-> tbl_employee AS emp
-> LEFT OUTER JOIN tbl_post AS post
-> ON emp.post_code = post.code
-> ORDER BY
-> emp.code
-> ;
+------------+----------+------------+--------+
| 社員コード | 社員名 | 役職コード | 役職名 |
+------------+----------+------------+--------+
| 101 | 伊藤英樹 | 1 | 部長 |
| 102 | 山本大樹 | 3 | 課長 |
| 103 | 中村千佳 | 4 | 係長 |
| 104 | 小林谷雄 | NULL | NULL |
| 105 | 斎藤美緒 | NULL | NULL |
+------------+----------+------------+--------+
5 rows in set (0.002 sec)

【テーブルが異なるカラム同士を結合して表示する】
worldデータベースに接続。
countryとcountrylanguageを結合し、英語圏の国の国名と言語名を一覧で表示されるようにする

MariaDB [world]> SELECT
-> Name AS "国名",
-> Language AS "言語名"
-> FROM
-> country,countrylanguage
-> WHERE
-> (Language = "English")
-> AND
-> (Code = CountryCode)
-> ORDER BY
-> Code;

・・・⑤に続く

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