样板
create database sky;
use sky;
create table m1(
id int(11),
name char(20),
age tinyint(10),
sex enum('男','女'),
score tinyint(10),
address char(20)
) default charset=utf8;
insert into m1 values
(1,'L1',21,'男',90,'北京'),
(2,'L2',19,'男',91,'上海'),
(3,'L3',24,'女',95,'广州'),
(4,'L4',22,'男',89,'广州'),
(5,'L5',20,'女',86,'上海'),
(6,'L6',19,'女',99,'广州');
执行顺序
1. select * from *
2. where 条件语句 #只筛选表里存在的字段
3. group by 分组
4. having 筛选
5. order by 排序高低
6. limit 显示几行,降序或者升序
group by
作用:给查询的结果进行分组,去重
1. group by 之后的字段必须为select之后的字段
2. 如果select 之后的字段和group by之后的字段不一致,则必须对select之后的字段做聚合处理。
筛选有几个城市
mysql> select address from m1 group by address;
+---------+
| address |
+---------+
| 上海 |
| 北京 |
| 广州 |
+---------+
3 rows in set (0.00 sec)
having
作用:对查询的结果进行进一步的筛选,只筛选通过计算出来的字段
1. having语句通常和group by语句联合使用,用来过滤group by语句返回的记录集
2. having语句的存在弥补了where关键字不能与聚合函数联合使用的不足
取出分数大于等于90前两名的城市
mysql> select address,avg(score) from m1
-> group by address
-> having avg(score) >= 90
-> order by avg(score) desc
-> limit 2;
+---------+------------+
| address | avg(score) |
+---------+------------+
| 广州 | 94.3333 |
| 北京 | 90.0000 |
+---------+------------+
2 rows in set (0.00 sec)
order by
作用:对查询的结果进行排序
语法: order by 字段名 排序方法
排序方式:
ASC(默认) : 升序
DESC:降序
从高到低进行排序
mysql> select * from m1 order by score desc;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 4 | L4 | 22 | 男 | 101 | 广州 |
| 6 | L6 | 19 | 女 | 99 | 北京 |
| 3 | L3 | 24 | 女 | 95 | 广州 |
| 2 | L2 | NULL | 男 | 91 | 上海 |
| 1 | L1 | 21 | 男 | 90 | 北京 |
| 5 | L5 | NULL | 女 | 86 | 上海 |
+------+------+------+------+-------+---------+
6 rows in set (0.00 sec)
显示address在北京和上海,age为20+,的人按score 升序
mysql> select * from m1
-> where
-> address in("北京","广州") and age like '2_'
-> order by score asc;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 4 | L4 | 22 | 男 | 89 | 广州 |
| 1 | L1 | 21 | 男 | 90 | 北京 |
| 3 | L3 | 24 | 女 | 95 | 广州 |
+------+------+------+------+-------+---------+
3 rows in set (0.00 sec)
limit
作用:限制显示查询记录的条数
语法:
limit n 显示n条记录
limit m,n 从第m+1条记录开始显示,显示n条
score字段降序排列,显示前三名
mysql> select * from m1 order by score desc;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 6 | L6 | 19 | 女 | 99 | 广州 |
| 3 | L3 | 24 | 女 | 95 | 广州 |
| 2 | L2 | 19 | 男 | 91 | 上海 |
| 1 | L1 | 21 | 男 | 90 | 北京 |
| 4 | L4 | 22 | 男 | 89 | 广州 |
| 5 | L5 | 20 | 女 | 86 | 上海 |
+------+------+------+------+-------+---------+
6 rows in set (0.00 sec)
mysql> select * from m1 order by score desc limit 3;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 6 | L6 | 19 | 女 | 99 | 广州 |
| 3 | L3 | 24 | 女 | 95 | 广州 |
| 2 | L2 | 19 | 男 | 91 | 上海 |
+------+------+------+------+-------+---------+
3 rows in set (0.00 sec)
socre字段降序从第二名开始显示四条
mysql> select * from m1 order by score desc limit 1,4;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 3 | L3 | 24 | 女 | 95 | 广州 |
| 2 | L2 | 19 | 男 | 91 | 上海 |
| 1 | L1 | 21 | 男 | 90 | 北京 |
| 4 | L4 | 22 | 男 | 89 | 广州 |
+------+------+------+------+-------+---------+
4 rows in set (0.00 sec)
查询age不为空,成绩前三的人
mysql> select * from m1;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 2 | L2 | NULL | 男 | 91 | 上海 |
| 3 | L3 | 24 | 女 | 95 | 广州 |
| 4 | L4 | 22 | 男 | 101 | 广州 |
| 5 | L5 | NULL | 女 | 86 | 上海 |
| 6 | L6 | 19 | 女 | 99 | 北京 |
| 1 | L1 | 21 | 男 | 90 | 北京 |
+------+------+------+------+-------+---------+
6 rows in set (0.00 sec)
mysql> select * from m1
-> where age is not null
-> order by score desc
-> limit 3;
+------+------+------+------+-------+---------+
| id | name | age | sex | score | address |
+------+------+------+------+-------+---------+
| 4 | L4 | 22 | 男 | 101 | 广州 |
| 6 | L6 | 19 | 女 | 99 | 北京 |
| 3 | L3 | 24 | 女 | 95 | 广州 |
+------+------+------+------+-------+---------+
3 rows in set (0.00 sec)