MYSQL之select的⾼级⽤法
作⽤:
# 多表联查,联表查询
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';
+--------+------+
html空格占位符代码
初始化一个长度为10的数组| 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.出两个表相关联的字段
# 4.编写语句
mysql> select country.name,city.name,city.population from country,city de and city.population < 100;
+----------+-----------+------------+
| name    | name      | population |
+----------+-----------+------------+
| Pitcairn | Adamstown |        42 |
+----------+-----------+------------+
1 row in set (0.01 sec)
svg在线生成器6.练习题⼆:连表查询:世界上⼩于100⼈的城市在哪个国家,是⽤什么语⾔?请列出城市名字,国家名字与⼈⼝数量和国家语⾔
# 1.确认我要查哪些内容
国家名字城市名字城市⼈⼝数量国家使⽤的语⾔⼩于100⼈
# 2.确认在哪个表
country.name  city.name  city.population  countrylanguage.language
# 3.出三个表相关联的字段
# 4.写sql语句
mysql> select country.name,city.name,city.population,countrylanguage.language from country,city,countrylanguage untrycode untrycode and city.population < 100; +----------+-----------+------------+-------------+
| name    | name      | population | language    |
+----------+-----------+------------+-------------+
| Pitcairn | Adamstown |        42 | Pitcairnese |
+----------+-----------+------------+-------------+
1 row in set (0.04 sec)
2.⾃连接
#⾃⼰查相同字段,使⽤⾃连接,两个关联的表必须有相同字段和相同数据
SELECT city.untrycode,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 条件;
thread类的所有方法#注意:命中率(驱动的概念)
表1 ⼩表
表2 ⼤表
select * from 表1 inner join 表2 on 相关联的条件 where 条件;
2.例⼦1:两表联查
#⼩于100⼈的城市在哪个国家,国家代码是什么?
select city.name,city.untrycode,country.name
from city join country de
where city.population < 100;
3.例⼦2:三表联查
#世界上⼩于100⼈的城市在哪个国家?是⽤什么语⾔?
select country.name,city.name,city.population,countrylanguage.language
from city join country de
倒伽马分布的分布函数join countrylanguage untrycode
where city.population < 100;
4.外连接(有问题)
1.左外连接
select city.untrycode,country.name,city.population
from city left join country
de
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.untrycode,country.name,city.population
from city right join country
de
and city.population < 100 limit 5;
+------+-------------+-------------+------------+
mysql面试题sql语句多表联查
| name | countrycode | name        | population |
+------+-------------+-------------+------------+
| NULL | NULL        | Aruba      |      NULL |
| NULL | NULL        | Afghanistan |      NULL |
| NULL | NULL        | Angola      |      NULL |
| NULL | NULL        | Anguilla    |      NULL |
| NULL | NULL        | Albania    |      NULL |
+------+-------------+-------------+------------+

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。