できるPRO MySQL できるPROシリーズ Kindle版の内容をまとめる。
INNER_JOIN
)INNER JOIN 結合するテーブル名 ON 条件
INNER JOIN 結合するテーブル名 USING(共通の列名)
USE world
MariaDB [dekirusample2]> USE world;
Database changed
SELECT Name, CountryCode FROM city LIMIT 3;
MariaDB [world]> SELECT Name, CountryCode FROM city LIMIT 3;
+----------+-------------+
| Name | CountryCode |
+----------+-------------+
| Kabul | AFG |
| Qandahar | AFG |
| Herat | AFG |
+----------+-------------+
3 rows in set (0.03 sec)
SELECT Name, ContryCode FROM country WHERE CountryCode = 'AFG';
MariaDB [world]> SELECT Name, Code FROM country WHERE Code = 'AFG';
+-------------+------+
| Name | Code |
+-------------+------+
| Afghanistan | AFG |
+-------------+------+
1 row in set (0.01 sec)
SELECT city.Name, country.Name
FROM city INNER JOIN country ON
city.CountryCode = country.Code
LIMIT 3;
MariaDB [world]> SELECT city.Name, country.Name FROM city INNER JOIN country ON city.CountryCode = country.Code LIMIT 3;
+------------+-------------+
| Name | Name |
+------------+-------------+
| Oranjestad | Aruba |
| Kabul | Afghanistan |
| Qandahar | Afghanistan |
+------------+-------------+
3 rows in set (0.00 sec)
SELECT city.Name AS city, country.Name As country
FROM city INNER JOIN country ON
city.CountryCode = country.Code
LIMIT 3;
MariaDB [world]> SELECT city.Name AS city, country.Name As country FROM city INNER JOIN country ON city.CountryCode = country.Code LIMIT 3;
+------------+-------------+
| city | country |
+------------+-------------+
| Oranjestad | Aruba |
| Kabul | Afghanistan |
| Qandahar | Afghanistan |
+------------+-------------+
3 rows in set (0.00 sec)
SELECT ci.Name AS city, co.Name As country
FROM city AS ci INNER JOIN country AS co ON
ci.CountryCode = co.Code
LIMIT 3;
MariaDB [world]> SELECT ci.Name AS city, co.Name As country FROM city AS ci INNER JOIN country AS co ON ci.CountryCode = co.Code LIMIT 3;
+------------+-------------+
| city | country |
+------------+-------------+
| Oranjestad | Aruba |
| Kabul | Afghanistan |
| Qandahar | Afghanistan |
+------------+-------------+
3 rows in set (0.00 sec)
SELECT ci.Name AS city, co.Name As country, ci.Population AS Population
FROM city AS ci INNER JOIN country AS co ON
ci.CountryCode = co.Code
WHERE ci.Population > 9000000;
MariaDB [world]> SELECT ci.Name AS city, co.Name As country, ci.Population AS Population FROM city AS ci INNER JOIN country AS co ON ci.CountryCode = co.Code
WHERE ci.Population > 9000000;
+-----------------+-------------+------------+
| city | country | Population |
+-----------------+-------------+------------+
| S?o Paulo | Brazil | 9968485 |
| Shanghai | China | 9696300 |
| Jakarta | Indonesia | 9604900 |
| Mumbai (Bombay) | India | 10500000 |
| Seoul | South Korea | 9981619 |
| Karachi | Pakistan | 9269265 |
+-----------------+-------------+------------+
6 rows in set (0.03 sec)
USE sakila
MariaDB [world]> USE sakila
Database changed
SELECT c.first_name, c.last_name
FROM rental AS r
INNER JOIN customer AS c
USING(customer_id)
WHERE return_date IS NULL
LIMIT 3;
MariaDB [sakila]> SELECT c.first_name, c.last_name FROM rental AS r INNER JOIN customer AS c USING(customer_id) WHERE return_date IS NULL LIMIT 3;
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| ELIZABETH | BROWN |
| MARGARET | MOORE |
| LISA | ANDERSON |
+------------+-----------+
3 rows in set (0.00 sec)