MySQL进阶查询——高级SQL语句(select)
MySQL进阶查询
在 MySQL 中,可以使用 select 语句来查询数据。
查询数据是指从数据库中根据需求,使用不同的查询方式来获取不同的数据,是使用频率最高、最重要的操作。
SELECT * FROM 表名;
SELECT 列名 FROM 表名 ;
#查询数据的一般用法,比较简单,今天给大家带来查询语句的高级语法。
按关键字排序
1、使用order by语句来实现排序
2、排序可针对一个或多个字段
3、ASC:升序,默认排序方式
4、DESC:降序
5、order by的语法结构:select 字段1,字段2 from 表名 order by 字段1 desc|asc,字段2 desc|asc;
举例:
1、排序前
mysql> select * from chengji;
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 1 | li | 77 |
| 2 | chen | 80 |
| 3 | zhang | 77 |
| 4 | han | 72 |
| 5 | xu | 95 |
+------+-------+-------+
5 rows in set (0.01 sec)
2、降序
mysql> select id,name,score from chengji order by score desc;
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 5 | xu | 95 |
| 2 | chen | 80 |
| 1 | li | 77 |
| 3 | zhang | 77 |
| 4 | han | 72 |
+------+-------+-------+
5 rows in set (0.00 sec)
3、升序
mysql> select id,name,score from chengji order by score asc;
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 4 | han | 72 |
| 1 | li | 77 |
| 3 | zhang | 77 |
| 2 | chen | 80 |
| 5 | xu | 95 |
+------+-------+-------+
5 rows in set (0.00 sec)
4、多字段排序
主要参考字段写在前面,辅助参考字段写在后面。先比较主要参考字段,如果相同,再比较辅助参考字段
mysql> select id,name,score from chengji order by score asc,id desc;
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 4 | han | 72 |
| 3 | zhang | 77 |
| 1 | li | 77 |
| 2 | chen | 80 |
| 5 | xu | 95 |
+------+-------+-------+
5 rows in set (0.00 sec)
对结果进行分组
1、使用group by语句来实现分组
2、通常结合聚合函数一起使用
3、可以按一个或多个字段对结果进行分组
举例:按score进行分组,count统计次数,score大于等于75的age相同的name数量
mysql> select count(name),score from chengji where score>=75 group by score;
+-------------+-------+
| count(name) | score |
+-------------+-------+
| 2 | 77 |
| 1 | 80 |
| 1 | 95 |
+-------------+-------+
3 rows in set (0.00 sec)
#按分数降序
mysql> select count(name),score from chengji where score>=75 group by score desc;
+-------------+-------+
| count(name) | score |
+-------------+-------+
| 1 | 95 |
| 1 | 80 |
| 2 | 77 |
+-------------+-------+
3 rows in set (0.00 sec)
限制结果条目
1、只返回select查询结果的第一行或前几行
2、使用limit语句限制条目
3、limit语法结构
(1)select 字段1,字段2 from 表名 limit [offset,] number;
(2)offset:位置偏移量,从0开始
(3)number:返回记录行的最大数目
举例:
mysql> select * from chengji limit 3; #显示三行数据
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 1 | li | 77 |
| 2 | chen | 80 |
| 3 | zhang | 77 |
+------+-------+-------+
3 rows in set (0.00 sec)
mysql> select * from chengji limit 1,3; #从第2行开始,显示三行,从0开始计数!!
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 2 | chen | 80 |
| 3 | zhang | 77 |
| 4 | han | 72 |
+------+-------+-------+
3 rows in set (0.00 sec)
mysql> select * from chengji order by score desc limit 3; #显示score最大的三行
+------+------+-------+
| id | name | score |
+------+------+-------+
| 5 | xu | 95 |
| 2 | chen | 80 |
| 1 | li | 77 |
+------+------+-------+
3 rows in set (0.00 sec)
设置别名
1、使用as语句设置别名,关键字as可省略
2、设置别名时,保证不能与库中其他表或字段名称冲突
3、别名的语法结构
举例:
字段别名:select 字段 as 别名 from 表名;
mysql> select name as user from chengji;
+-------+
| user |
+-------+
| li |
| chen |
| zhang |
| han |
| xu |
+-------+
5 rows in set (0.00 sec)
通配符
1、用于替换字符串中的部分字符
2、通常配合like一起使用,并协同where完成查询
3、常用通配符
(1)%表示零个、一个或多个即任意字符
(2)_表示单个字符
举例:
mysql> select * from chengji where name like 'h%'; #匹配“name”里以h开头的任意字符串
+------+------+-------+
| id | name | score |
+------+------+-------+
| 4 | han | 72 |
+------+------+-------+
1 row in set (0.01 sec)
mysql> select * from chengji where name like '_h%'; #匹配“name”里以“任意一个字符+h+任意字符”的字符串
+------+-------+-------+
| id | name | score |
+------+-------+-------+
| 2 | chen | 80 |
| 3 | zhang | 77 |
+------+-------+-------+
2 rows in set (0.00 sec)
子查询
1、也称作内查询或者嵌套查询
2、先于主查询被执行,其结果将作为外层主查询的条件
3、在增删改查中都可以使用子查询
4、支持多层嵌套
5、in语句用来判断某个值是否在给定的结果集中
举例:
mysql> select name,score from chengji where score in(72,77); #查看name,score,只看score为72,77的值
+-------+-------+
| name | score |
+-------+-------+
| li | 77 |
| zhang | 77 |
| han | 72 |
+-------+-------+
3 rows in set (0.00 sec)