参考:
MySQL 连接的使用
SQL 连接
导入 World.sql
导入一个 World 数据库,点击下载,解压即可
传统连接查询(WHERE)
1. 连表查询:世界上小于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)
2. 连表查询:世界上小于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)
3. 连表查询:统计中国共有多少个省份,显示中国英文缩写,使用语言
# 1.确认我要查哪些内容
国家名字 国家名称缩写 省份数量(统计处于同一个国家的省份的数量,需要去除重复的 district) 使用语言
country.name city.countrycode count(distinct city.district) countrylanguage.language
# 2.找出三个表相关联的字段
country.code city.countrycode countrylanguage.countrycode
mysql> select b.name,a.countrycode,count(distinct a.district),c.language from city a,country b,countrylanguage c where a.countrycode="CHN" and a.countrycode=c.countrycode and a.countrycode=b.code group by a.countrycode;
+-------+-------------+----------------------------+----------+
| name | countrycode | count(distinct a.district) | language |
+-------+-------------+----------------------------+----------+
| China | CHN | 31 | Chinese |
+-------+-------------+----------------------------+----------+
1 row in set (0.00 sec)
自然连接(NATURAL JOIN)
# 自己查找相同字段,使用自连接,两个关联的表必须有相同字段和相同数据
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.两个表中的数据必须完全相同
内连接(INNER JOIN)
INNER JOIN
或 JOIN
,两张表连接查询时,只保留两张表中匹配的(ON
子句定义的相关连的条件相同)的记录
# 语法格式
select * from 表1 join 表2 on 相关联的条件 where 条件;
select * from 表1 inner join 表2 on 相关联的条件 where 条件;
# 注意:命中率(驱动的概念)
表1 小表
表2 大表
# 小于100人的城市在哪个国家,国家代码是什么?
select city.name,city.population,city.countrycode,country.name
from city join country on city.countrycode=country.code
where city.population < 100;
# 相同功能的 WHERE 语句
select city.name,city.population,city.countrycode,country.name
from city,country where city.countrycode=country.code
and city.population < 100;
# 世界上小于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;
外连接(OUTER JOIN)
左外连接(LEFT JOIN)
LEFT JOIN
,在两张表进行连接查询时,会返回左表所有的行,即使在右表中没有匹配的记录,右表中没有的值显示为 NULL
:
select city.name,city.countrycode,country.name,city.population
from city left join country
on city.countrycode=country.code;
右外连接(RIGHT JOIN)
RIGHT JOIN
,在两张表进行连接查询时,会返回右表所有的行,即使在左表中没有匹配的记录,左表中没有的值显示为 NULL
:
select city.name,city.countrycode,country.name,city.population
from city right join country
on city.countrycode=country.code
and city.population < 100;
RUNOOB 示例
参考:
MySQL 连接的使用
SQL 连接
ON 和 WHERE 的区别
参考:
ON 和 WHERE 的区别
ON 条件放在 INNER JOIN 语句中,和 WHERE 条件并没有什么不同(结果,不提效率),
ON 条件放在 LEFT JOIN 或者 RIGHT JOIN 语句中,和 WHERE 条件不同:
ON 条件会显示右表(LEFT JOIN)或左表(RIGHT JOIN)的全部内容,而 WHERE 会对全部内容筛选,显示部分内容 。
左连接,即使 ON 子句条件不为真,也会返回右表中的内容(显示为 NULL);而 WHERE 子句条件不为真,不会返回右表中的内容:
右连接,即使 ON 子句条件不为真,也会返回左表中的内容(显示为 NULL);而 WHERE 子句条件不为真,不会返回左表中的内容:
小结:
ON 条件是在生成临时表时使用的条件 。不管 ON 中的条件是否为真,都会返回左表(LEFT JOIN)或右表(RIGHT JOIN)中的全部记录 。
WHERE 条件是在临时表生成好后,再对临时表进行过滤的条件,不会返回全部记录,只会返回符合 WHERE 条件的记录 。