快捷搜索:  汽车  科技

select后面的嵌套查询(select查询操作与联合查询)

select后面的嵌套查询(select查询操作与联合查询)union all 不做去除重复处理union会做去除重复处理,性能比union会差一些select district sum(population) from city where countrycode='CHN' group by district order by sum(population) desc;统计中国,每个省的总人口,找出总人口大于500万的,并按总人口从大到小排序,只显示前三名select district sum(population) from city where countrycode='CHN' group by district having sum(population) > 5000000 order by sum(population) DESC limit 3;union和union allunion 和un

大家好,我是anyux。上一节本节是对于SQL语句select详细演示操作 本节是对于select练习演示操作。

select后面的嵌套查询(select查询操作与联合查询)(1)

统计中国每个省的总人口数,打印总人口数小于100万的

use world;select district sum(population) from city where countrycode='CHN' group by district havingsum(population)>1000000;

查看中国所有的城市,并按人口数排序(从大到小)

select name from city where countrycode='CHN' order by population desc;

统计中国每个省的总人口数量,按照总人口从大到小排序

select district sum(population) from city where countrycode='CHN' group by district order by sum(population) desc;

统计中国,每个省的总人口,找出总人口大于500万的,并按总人口从大到小排序,只显示前三名

select district sum(population) from city where countrycode='CHN' group by district having sum(population) > 5000000 order by sum(population) DESC limit 3;

select后面的嵌套查询(select查询操作与联合查询)(2)

union和union all

union 和union all 的区别

union会做去除重复处理,性能比union会差一些

union all 不做去除重复处理

作用:多个结果集合并查询的功能

查询中国或美国的城市信息

select * from city where countrycode in ("CHN" 'USA');

改写后的SQL语句,索引等级会提高

select * from city where countrycode='CHN' union all select * from city where countrycode="USA";

统计中国,每个省的总人口,找出总人口大于500万的,并按总人口从大到小排序,显示前三名和后三名

注意,如果这里使用了子查询,因为两个查询结果都使用了order by 子句,为排除报错使用子查询

select * from ( select district sum(population) from city where countrycode='CHN' group by district having sum(population) > 5000000 order by sum(population) desc limit 3 ) m union all select * from ( select district sum(population) from city where countrycode="CHN" group by district having sum(population) > 5000000 order by sum(population) asc limit 3) n;

select后面的嵌套查询(select查询操作与联合查询)(3)

多表连接查询(内连接)

基本语法

  1. 最核心的是找到多张表之间的关联条件列,比如下面示例中city表的countrycode和country表中的code相关联
  2. 列书写时,格式必须为 表名.列,因为如果不标识出来,就无法确定是哪个表的列进行关联
  3. 表的关联列使用=(等号)连接如city.countrycode=country.code
  4. 要查询的数据也必须将列前面加上表名称
  5. 关联条件时使用on连接条件 如(from city join country on city.countrycode=country.code)
  6. 将所有的过滤、分组、排序等条件按顺序写在on的后面
  7. 多张表关联A JOIN B on A.x=B.y JOIN B C on B.m=C.n
  8. 建议使用数据少的表做为连接的驱动表,将数据行最少的放在最左边,后续所有表的关联列尽量是主键或唯一键,至少建立一个索引

查询世界上小于100人的城市,所在的国家名,国土面积,城市名,人口数

select country.name country.surfaceArea city.name city.population from city join country on city.countrycode=country.code where city.population < 100;

select后面的嵌套查询(select查询操作与联合查询)(4)

使用别名做简化处理

select B.name B.surfaceArea A.name A.population from city as A join country as B on A.countrycode=B.code where A.population < 100;

查询城市shenyang,城市人口,所在国家名(name)及国土面积(SurfaceArea)

select A.name A.population B.name B.surfacearea from city as A join country as B on A.countrycode = B.code where A.name='shenyang';​

select后面的嵌套查询(select查询操作与联合查询)(5)

别名

Mysql别名分为列别名,和表别名

表别名可以出现在任意子句中。列别名可以使用ORDER BY,GROUP BY和HAVING子句中的列别名来引用该列

注意:不能在WHERE子句中使用列别名。原因是当MySQL评估求值WHERE子句时,SELECT子句中指定的列的值可能尚未确定。

select A.name as 城市名称 A.population as 城市人口 B.name as 国家名称 B.surfacearea as 国家人口 from city as A join country as B on A.countrycode = B.code where A.name='shenyang';

select后面的嵌套查询(select查询操作与联合查询)(6)

本节是对于SQL语句select练习演示。其中如有不足还各位同学指出。下一节将进行更详细的复杂的select练习操作。

如果有关于MySQL的相关问题,请扫描下方二维码,联系我

select后面的嵌套查询(select查询操作与联合查询)(7)

猜您喜欢: