樣板mysql
create database sky;sql
use sky;ide
create table m1(函數
id int(11),spa
name char(20),排序
age tinyint(10),it
sex enum('男','女'),table
score tinyint(10),class
address char(20)select
) 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)