MYSQL之select的高階用法
阿新 • • 發佈:2020-07-17
作用:
# 多表聯查,聯表查詢
1.傳統連線
1.集合 #集合 [xiaoqiu,xiaowang,qiandao] [80,90,100] #資料庫 id:[1,2,3] name:[xiaoqiu,xiaowang,qiandao] id:[1,2,3] mark:[80,90,100] 2.建表 mysql> create table students(id int,name varchar(10)); Query OK, 0 rows affected (0.08 sec) mysql> create table score(id int,mark int); Query OK, 0 rows affected (0.05 sec) 3.插入資料 mysql> insert into students values(1,'xiaoqiu'),(2,'qianqian'),(3,'xiaowang'); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> insert into score values(1,80),(2,90),(3,100); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 4.資料查詢 # 檢視兩個表的資料 mysql> select * from students; +------+---------+ | id | name | +------+---------+ | 1 | xiaoqiu | | 2 | qiandao | | 3 | xiaowang| +------+---------+ 3 rows in set (0.00 sec) mysql> select * from score; +------+------+ | id | mark | +------+------+ | 1 | 80 | | 2 | 90 | | 3 | 100 | +------+------+ 3 rows in set (0.00 sec) # 檢視xiaoqiu的分數 mysql> select students.name,score.mark from students,score where students.id=1 and score.id=1; mysql> select students.name,score.mark from students,score where students.id=score.id and name='xiaoqiu'; +--------+------+ | name | mark | +--------+------+ | xiaoqiu | 80 | +--------+------+ 1 row in set (0.01 sec) # 查詢所有學生成績 mysql> select students.name,score.mark from students,score where students.id=score.id 5.連表查詢:世界上小於100人的城市在哪個國家?請列出城市名字,國家名字與人口數量 # 1.確認我要查哪些內容 國家名字 城市名字 城市人口數量 小於100人 # 2.確認在哪個表 country.name city.name city.population # 3.找出兩個表相關聯的欄位 city.countrycode country.code # 4.編寫語句 mysql> select country.name,city.name,city.population from country,city where city.countrycode=country.code and city.population < 100; +----------+-----------+------------+ | name | name | population | +----------+-----------+------------+ | Pitcairn | Adamstown | 42 | +----------+-----------+------------+ 1 row in set (0.01 sec) 6.練習題二:連表查詢:世界上小於100人的城市在哪個國家,是用什麼語言?請列出城市名字,國家名字與人口數量和國家語言 # 1.確認我要查哪些內容 國家名字 城市名字 城市人口數量 國家使用的語言 小於100人 # 2.確認在哪個表 country.name city.name city.population countrylanguage.language # 3.找出三個表相關聯的欄位 country.code city.countrycode countrylanguage.countrycode # 4.寫sql語句 mysql> select country.name,city.name,city.population,countrylanguage.language from country,city,countrylanguage where country.code=city.countrycode and city.countrycode=countrylanguage.countrycode and city.population < 100; +----------+-----------+------------+-------------+ | name | name | population | language | +----------+-----------+------------+-------------+ | Pitcairn | Adamstown | 42 | Pitcairnese | +----------+-----------+------------+-------------+ 1 row in set (0.04 sec)
2.自連線
#自己查詢相同欄位,使用自連線,兩個關聯的表必須有相同欄位和相同資料 SELECT city.name,city.countrycode,countrylanguage.language,city.population FROM city NATURAL JOIN countrylanguage WHERE population > 1000000 ORDER BY population; #兩個表中沒有相同欄位不行,欄位相同值不同不行 SELECT country.name,city.name,city.population FROM city NATURAL JOIN country WHERE population < 100; #注意: 1.自連線必須有相同欄位和相同值 2.兩個表中的資料必須完全相同
3.內連線
1.語法格式
select * from 表1 join 表2 on 相關聯的條件 where 條件;
#注意:命中率(驅動的概念)
表1 小表
表2 大表
select * from 表1 inner join 表2 on 相關聯的條件 where 條件;
2.例子1:兩表聯查
#小於100人的城市在哪個國家,國家程式碼是什麼? select city.name,city.population,city.countrycode,country.name from city join country on city.countrycode=country.code where city.population < 100;
3.例子2:三表聯查
#世界上小於100人的城市在哪個國家?是用什麼語言?
select country.name,city.name,city.population,countrylanguage.language
from city join country on city.countrycode=country.code
join countrylanguage on country.code=countrylanguage.countrycode
where city.population < 100;
4.外連線(有問題)
1.左外連線
select city.name,city.countrycode,country.name,city.population
from city left join country
on city.countrycode=country.code
and city.population < 100 limit 5;
+----------------+-------------+------+------------+
| name | countrycode | name | population |
+----------------+-------------+------+------------+
| Kabul | AFG | NULL | 1780000 |
| Qandahar | AFG | NULL | 237500 |
| Herat | AFG | NULL | 186800 |
| Mazar-e-Sharif | AFG | NULL | 127800 |
| Amsterdam | NLD | NULL | 731200 |
+----------------+-------------+------+------------+
2.右外連線
select city.name,city.countrycode,country.name,city.population
from city right join country
on city.countrycode=country.code
and city.population < 100 limit 5;
+------+-------------+-------------+------------+
| name | countrycode | name | population |
+------+-------------+-------------+------------+
| NULL | NULL | Aruba | NULL |
| NULL | NULL | Afghanistan | NULL |
| NULL | NULL | Angola | NULL |
| NULL | NULL | Anguilla | NULL |
| NULL | NULL | Albania | NULL |
+------+-------------+-------------+------------+