0
点赞
收藏
分享

微信扫一扫

5DQL之mysql单表查询语句select

select应用场景:

1单独使用;

查看端口号:select @@port;

查看用户和主机:select user,host from mysql.user;

格式化查看用户和主机:select user,host from mysql.user\G;

模糊查询含有innodb的命令:show variables like '%innodb%'

 2配合函数使用:

查时间:select now();

查询当前所在的库: select database();

查询库名:
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 47
Current database: *** NONE ***

+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| tangTest |
| testku |
+--------------------+
6 rows in set (0.04 sec)
使用mysql这个库:
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

查看当前的库名:
mysql> select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)

查看当前数据库版本:

mysql> select version();
+------------+
| version() |
+------------+
| 5.7.26-log |
+------------+
1 row in set (0.00 sec)

mysql>

SQL92标准的使用语法
select语法执行顺序:
select开始--->from子句--->where子句--->group by子句--->select后执行条件--->having子句--->order by --->limit

--- select语句应用

5DQL之mysql单表查询语句select_相对路径


1进入world这个库,查询city表的所以数据:

相对路径写法:
use world;
select * from city;
绝对路径写法:
select * from world.city;

2查询name和populaton这两个列的所有值:

相对路径写法:
use world
select name, population from city;
绝对路径写法:
select name, population from world.city;

3查询库里所有的表:

show tables from world;

4列出表中的信息:

DSC city;

---where语句用法:
1等值查询(=)
查询中国城市信息

select * from world.city where countrycode='CHN';

2不等值查询(>,<,>=,<=,<>)
查询世界人口小于100人的城市

select * from world.city where population<100;

3模糊查询(需配合like)
查询国家代号是c开头的城市:

select * from world.city where countrycode like 'c%';

统计国家代码以c开头的城市一共有多少行?

select count(*) from world.city where countrycode like 'c%';

4配合逻辑连接符查询(and ,or)
统计大于等于1万小于等于3万的城市

select * from world.city where population >= 10000 and population <= 20000;
select * from world.city where population between 10000 and 20000;

5查询一下中国或美国的城市信息

select * from world.city where countrycode='CHN' or countrycode='USA';
select * from world.city where countrycode in ('CHN','USA');

6查询一下中国或美国的城市信息(推荐):
union和union all的意思是聚合;但union可以去重,union all不去重。

select * from world where countrycode='CHN' union all select * from world.city where countrycode='USA'

-- group by 配合聚合函数应用
常用的聚合函数:
avg()
count()
max()
min()
group_concat()

1统计世界上每个国家的人口数:
解释:以国家代码进行分组,以国家代码进行聚合运算

select countrycode,sum(population) from world.city group by countrycode;

2统计每个国家的城市个数
字段name是城市名的字段

select countrycode, count(name) from world.city group by countrycode;

3统计每个国家的省名列表
count()函数表示将多个字符串连接成一个字符串,而group_concat()则是以group by为条件拼接字符串
这里的district是省的字段名

5DQL之mysql单表查询语句select_升序_02

select coutrycode,group_concat(district) from world.city group_by countrycode;

4统计中国每个省的城市名列表

select district, group_concat(name) from worls.city where countrycode='CHN' group by district;

5统计中国每个省的总人口数

select discrit, sum(population) from world.city where countrycode='CHN' gropy by district;

6统计一下中国每个省的平均人口数

select dictrict, avg(population) from world.city where countrycode='CHN' group by district;

 -- having:对处理的结果进行再过滤(后过滤):

统计中国的每个省的人口数量大于1000万的省人口数

select district,sum(population) from world.city where countrycode='CHN' group by district having sum(population)>10000000;

 

统计中国每个省的总人口数并从小到大排序

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

从大到小排列中国每个省的人口数

select district sum(population) from world.city where countrycode='CHN' group by district order by dum(population) 
DESC;

查询中国所有城市,并以人口数降序排列

select * from world.city where countrycode='CHN' order by popelation DESC;

 -- limit:分页显示(一般都配合order by一起使用):

 查询中国所有城市,以人口数量升序排列,只显示前五

select * from world.city where countrycode='CHN' order by population limit 5;

查询中国所有城市,以人口数量升序排列,显示6-8行

limit 5,5:第一个5表示跳过前5行,第二个3表示显示3行,其实就是显示6-8行。

select * from world.city where countrycode='CHN' order by population limit 5,3;

查询中国所有城市,以人口数量升序排列,显示6-10行

limit 5 offset 5:第一个5表示显示5行,第二个5表示偏移5行

select * from world.city where countrycode='CHN' order by population limit 5 offset 5;

 



举报

相关推荐

0 条评论