常用查询
(增、删、改、查)对 MySQL 数据库的查询,除了基本的查询外,有时候需要对查询的结果集进行处理。
一 按关键字排序
类比于windows 任务管理器
使用 SELECT 语句可以将需要的数据从 MySQL 数据库中查询出来,
如果对查询的结果进行排序,可以使用 ORDER BY 语句来对语句实现排序,
语法
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ...
ASC|DESC:
ASC :按照升序进行排序,是默认的排序方式,即 ASC 可以省略,SELECT 语句中如果没有指定具体的排序方式,则默认按 ASC方式进行排序。
DESC :按降序排序。ORDER BY 前面也可以使用 WHERE 子句对查询结果进一步过滤。
实操:
mysql> use kgc_ky35;
mysql> show tables;
Empty set (0.00 sec)
mysql> create table HP (id int(10),name varchar(16) primary key not null,score decimal(5,2),address varchar(40),hobby int(8));
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> insert into HP values (1,'hz',90,'tianye',3);
Query OK, 1 row affected (0.01 sec)
mysql> insert into HP values (2,'mdq',80,'renduo',3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into HP values (2,'cx',60,'guancai',3);
Query OK, 1 row affected (0.01 sec)
mysql> select * from HP;
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
①按分数排序,默认不指定是升序排列
mysql> select id,name,score from HP order by score;
+------+------+-------+
| id | name | score |
+------+------+-------+
| 2 | cx | 60.00 |
| 2 | mdq | 80.00 |
| 1 | hz | 90.00 |
+------+------+-------+
3 rows in set (0.00 sec)
②分数按降序排列
mysql> select id,name,score from HP order by score desc;
+------+------+-------+
| id | name | score |
+------+------+-------+
| 1 | hz | 90.00 |
| 2 | mdq | 80.00 |
| 2 | cx | 60.00 |
+------+------+-------+
3 rows in set (0.00 sec)
③order by还可以结合where进行条件过滤,筛选地址是'renduo'的学生按分数降序排列
mysql> select name,score from HP where address='renduo' order by score desc;
+------+-------+
| name | score |
+------+-------+
| mdq | 80.00 |
+------+-------+
1 row in set (0.00 sec)
④查询学生信息先按兴趣id降序排列,相同分数的,id也按降序排列
mysql> select id,name,hobby from HP order by hobby desc,id desc;
+------+------+-------+
| id | name | hobby |
+------+------+-------+
| 2 | mdq | 3 |
| 2 | cx | 3 |
| 1 | hz | 3 |
+------+------+-------+
3 rows in set (0.00 sec)
2 区间判断及查询不重复记录
① AND/OR ------且/或mysql> select * from HP where score >70 and score <=90;
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
+------+------+-------+---------+-------+
2 rows in set (0.00 sec)
②mysql> select * from HP where score >60 or score <=90;
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
| 2 | cx | 60.00 | guancai | 3 |
+------+------+-------+---------+-------+
3 rows in set (0.00 sec)
3 嵌套/多条件
mysql> select * from HP where score >70 or (score >75 and score <90);
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
+------+------+-------+---------+-------+
2 rows in set (0.00 sec)
4 distinct 查询不重复记录
语法: select distinct 字段 from 表名﹔
mysql> select distinct hobby from HP;
+-------+
| hobby |
+-------+
| 3 |
+-------+
1 row in set (0.00 sec)
5 对结果进行分组
通过 SQL 查询出来的结果,还可以对其进行分组,使用 GROUP BY 语句来实现 ,GROUP BY 通常都是结合聚合函数一起使用的,常用的聚合函数包括:计数(COUNT)、 求和(SUM)、求平均数(AVG)、最大值(MAX)、最小值(MIN),
语法
SELECT column_name, aggregate_function(column_name)FROM table_name WHERE column_name operator value GROUP BY column_name;
①按hobby相同的分组,计算相同分数的学生个数(基于name个数进行计数)
mysql> select count(name),hobby from HP group by hobby;
+-------------+-------+
| count(name) | hobby |
+-------------+-------+
| 3 | 3 |
+-------------+-------+
1 row in set (0.00 sec)
②结合where语句,筛选分数大于等于80的分组,计算学生个数
mysql> select count(name),hobby from HP where score>=80 group by hobby;
+-------------+-------+
| count(name) | hobby |
+-------------+-------+
| 2 | 3 |
+-------------+-------+
1 row in set (0.00 sec)
③结合order by把计算出的学生个数按升序排列
mysql> select count(name),score,hobby from HP where score>=70 group by hobby order by count(name) asc;
+-------------+-------+-------+
| count(name) | score | hobby |
+-------------+-------+-------+
| 2 | 90.00 | 3 |
+-------------+-------+-------+
1 row in set (0.01 sec)
④取最小值
mysql> select min(score) from HP;
+------------+
| min(score) |
+------------+
| 60.00 |
+------------+
1 row in set (0.00 sec)
二 限制结果条目
limit 限制输出的结果记录
在使用 MySQL SELECT 语句进行查询时,结果集返回的是所有匹配的记录(行)。有时候仅 需要返回第一行或者前几行,这时候就需要用到 LIMIT 子句
语法
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number
LIMIT 的第一个参数是位置偏移量(可选参数),是设置 MySQL 从哪一行开始显示。 如果不设定第一个参数,将会从表中的第一条记录开始显示。需要注意的是,第一条记录的 位置偏移量是 0,第二条是 1,以此类推。第二个参数是设置返回记录行的最大数目。
举例:
①查询所有信息显示前2行记录
mysql> select * from HP limit 2;
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
+------+------+-------+---------+-------+
2 rows in set (0.00 sec)
②从第1行开始,往后显示1行内容
mysql> select * from HP limit 1,1;
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 2 | mdq | 80.00 | renduo | 3 |
+------+------+-------+---------+-------+
1 row in set (0.00 sec)
③结合order by语句,按id的大小升序排列显示前三行
mysql> select id,name from HP order by id limit 3;
+------+------+
| id | name |
+------+------+
| 1 | hz |
| 2 | mdq |
| 2 | cx |
+------+------+
3 rows in set (0.01 sec)
④基础select 小的升阶 怎么输出最后三行
mysql> select id,name from HP order by id desc limit 3;
+------+------+
| id | name |
+------+------+
| 2 | mdq |
| 2 | cx |
| 1 | hz |
+------+------+
3 rows in set (0.00 sec)
三 设置别名(alias ------as)
在 MySQL 查询时,当表的名字比较长或者表内某些字段比较长时,为了方便书写或者 多次使用相同的表,可以给字段列或表设置别名。使用的时候直接使用别名,简洁明了,增强可读性
语法
对于列的别名:SELECT column_name AS alias_name FROM table_name;
对于表的别名:SELECT column_name(s) FROM table_name AS alias_name;
在使用 AS 后,可以用 alias_name 代替 table_name,其中 AS 语句是可选的。AS 之后的别名,主要是为表内的列或者表提供临时的名称,在查询过程中使用,库内实际的表名 或字段名是不会被改变的
举例:
select name as 姓名,score as 成绩 from HP;
mysql> select name as 姓名,score 成绩,address 地址 from HP;
+--------+--------+---------+
| 姓名 | 成绩 | 地址 |
+--------+--------+---------+
| hz | 90.00 | tianye |
| mdq | 80.00 | renduo |
| cx | 60.00 | guancai |
+--------+--------+---------+
3 rows in set (0.00 sec)
如果表的长度比较长,可以使用 AS 给表设置别名,在查询的过程中直接使用别名
临时设置info的别名为i
select i.name as 姓名,i.score as 成绩 from info as i;
①查询info表的字段数量,以number显示
mysql> select count(*) as number from HP;
+--------+
| number |
+--------+
| 3 |
+--------+
1 row in set (0.00 sec)
②不用as也可以,一样显示
mysql> select count(*) number from HP;
+--------+
| number |
+--------+
| 3 |
+--------+
1 row in set (0.00 sec)
mysql> select k.name as 姓名,score 成绩,k.address 地址 from HP as k;
+--------+--------+---------+
| 姓名 | 成绩 | 地址 |
+--------+--------+---------+
| hz | 90.00 | tianye |
| mdq | 80.00 | renduo |
| cx | 60.00 | guancai |
+--------+--------+---------+
3 rows in set (0.00 sec)
mysql> select sum(score) 总分数 from HP;
+-----------+
| 总分数 |
+-----------+
| 230.00 |
+-----------+
1 row in set (0.00 sec)
使用场景:
1 对复杂的表进行查询的时候,别名可以缩短查询语句的长度
2 多表相连查询的时候(通俗易懂、减短sql语句)
此外,AS 还可以作为连接语句的操作符。
mysql> create table test01 as select * from HP;
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> desc test01;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(16) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| hobby | int(8) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> create table test02 (select * from HP);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> desc test02;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| name | varchar(16) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| hobby | int(8) | YES | | NULL | |
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> create table test03 as select * from HP where score >=70;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test03;
+------+------+-------+---------+-------+
| id | name | score | address | hobby |
+------+------+-------+---------+-------+
| 1 | hz | 90.00 | tianye | 3 |
| 2 | mdq | 80.00 | renduo | 3 |
+------+------+-------+---------+-------+
2 rows in set (0.00 sec)
四 通配符
通配符主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来。
通常通配符都是跟 LIKE 一起使用的,并协同 WHERE 子句共同来完成查询任务。常用的通配符有两个,
%:百分号表示零个、一个或多个字符
_:下划线表示单个字符
①查询名字是h开头的记录
mysql> select id,name from HP where name like 'h%';
+------+------+
| id | name |
+------+------+
| 1 | hz |
+------+------+
1 row in set (0.00 sec)
②查询名字里是hz中间有一个字符的记录
mysql> select id,name from HP where name like 'hz';
+------+------+
| id | name |
+------+------+
| 1 | hz |
+------+------+
1 row in set (0.00 sec)
③查询名字中间有x的记录
mysql> select id,name from HP where name like '%x%';
+------+------+
| id | name |
+------+------+
| 2 | cx |
+------+------+
1 row in set (0.00 sec)
④查询m后面2个字符的名字记录
mysql> select id,name from HP where name like 'm__';
+------+------+
| id | name |
+------+------+
| 2 | mdq |
+------+------+
1 row in set (0.00 sec)
⑤通配符"%"和"_"不仅可以单独使用,也可以组合查询名字以h开头的记录
mysql> select id,name from HP where name like 'h%_';
+------+------+
| id | name |
+------+------+
| 1 | hz |
+------+------+
1 row in set (0.00 sec)