MySQL查询语句使用总结(一)——单表查询
从表中查询所需要的数据使用select命令,select命令的语法如下:
select [distinct] 字段列表 [as 字段别名]
from 数据源 [join 数据源 on 连接条件]
[where子句]
[group by 子句]
[having子句]
[order by 子句]
[limit子句];
一、数据准备: 创建emp(员工)表,代码如下:
create table emp(
e_id int primary key auto_increment comment '员工编号',
e_name char(20) comment '姓名',
birth datetime comment '出生日期',
phone char(20) comment '电话',
salary decimal(10,2) comment '工资',
department char(20) comment '部门'
);
为emp(员工)表插入必要的数据,命令如下:
insert into emp(e_name,birth,phone,salary,department)
values('张平','1990-1-23','13607589999',5000,'人事部'),('张静静','1994-10-3','13803732222',4500,'人事部'),
('刘鹏飞','1989-10-15','13903735874',5800,'人事部'),('王晓云','1992-3-30','13703736666',6600,'销售部'),
('张红刚','1989-3-25','13603736446',8000,'销售部'),('刘小贝','1997-12-5','13803731234',6700,'销售部'),
('王贝贝','1979-1-30','13503735888',9500,'销售部'),('王云飞','1984-3-12','13603734848',9200,'销售部'),
('刘达龙','1989-6-6','13703736699',5900,'财务部'),('刘胖胖','1992-11-3','13503736622',6800,'财务部');
二、查看表中的所有内容,这是最基本的select命令,命令如下:
mysql> select * from emp;
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 1 | 张平 | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | 人事部 |
| 2 | 张静静 | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | 人事部 |
| 3 | 刘鹏飞 | 1989-10-15 00:00:00 | 13903735874 | 5800.00 | 人事部 |
| 4 | 王晓云 | 1992-03-30 00:00:00 | 13703736666 | 6600.00 | 销售部 |
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
| 6 | 刘小贝 | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | 销售部 |
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
| 8 | 王云飞 | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | 销售部 |
| 9 | 刘达龙 | 1989-06-06 00:00:00 | 13703736699 | 5900.00 | 财务部 |
| 10 | 刘胖胖 | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | 财务部 |
+------+-----------+---------------------+-------------+---------+------------+
10 rows in set (0.00 sec)
三、查看表中的部分字段并为字段指定别名,命令如下:
mysql> select e_name as 员工姓名,birth as 员工生日,salary as 员工工资 from emp;
+--------------+---------------------+--------------+
| 员工姓名 | 员工生日 | 员工工资 |
+--------------+---------------------+--------------+
| 张平 | 1990-01-23 00:00:00 | 5000.00 |
| 张静静 | 1994-10-03 00:00:00 | 4500.00 |
| 刘鹏飞 | 1989-10-15 00:00:00 | 5800.00 |
| 王晓云 | 1992-03-30 00:00:00 | 6600.00 |
| 张红刚 | 1989-03-25 00:00:00 | 8000.00 |
| 刘小贝 | 1997-12-05 00:00:00 | 6700.00 |
| 王贝贝 | 1979-01-30 00:00:00 | 9500.00 |
| 王云飞 | 1984-03-12 00:00:00 | 9200.00 |
| 刘达龙 | 1989-06-06 00:00:00 | 5900.00 |
| 刘胖胖 | 1992-11-03 00:00:00 | 6800.00 |
+--------------+---------------------+--------------+
10 rows in set (0.00 sec)
四、带where条件的查询:
关系运算符:>, <, =, >=, <=, !(<>)
逻辑运算符:and, or
其他运算符:between…and…,in,not in,is null,is not null
mysql> select * from emp
where salary>=7000 and department='销售部'; --查询销售部工资超过7000的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
| 8 | 王云飞 | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp
where salary>7000 or salary<5000; --查询工资高于7000或低于5000的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 2 | 张静静 | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | 人事部 |
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
| 8 | 王云飞 | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
4 rows in set (0.00 sec)
mysql> select * from emp
where salary between 6000 and 7000; --查询工资在6000与7000之间的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 4 | 王晓云 | 1992-03-30 00:00:00 | 13703736666 | 6600.00 | 销售部 |
| 6 | 刘小贝 | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | 销售部 |
| 10 | 刘胖胖 | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | 财务部 |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp
where salary not between 5000 and 7000; --查询工资不在5000与7000之间的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 2 | 张静静 | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | 人事部 |
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
| 8 | 王云飞 | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
4 rows in set (0.00 sec)
mysql> select * from emp
where department in ('人事部','财务部'); --查询人事部和财务部的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 1 | 张平 | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | 人事部 |
| 2 | 张静静 | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | 人事部 |
| 3 | 刘鹏飞 | 1989-10-15 00:00:00 | 13903735874 | 5800.00 | 人事部 |
| 9 | 刘达龙 | 1989-06-06 00:00:00 | 13703736699 | 5900.00 | 财务部 |
| 10 | 刘胖胖 | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | 财务部 |
+------+-----------+---------------------+-------------+---------+------------+
5 rows in set (0.00 sec)
mysql> select * from emp
where department not in ('人事部','财务部'); --查询不是人事部和财务部的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 4 | 王晓云 | 1992-03-30 00:00:00 | 13703736666 | 6600.00 | 销售部 |
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
| 6 | 刘小贝 | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | 销售部 |
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
| 8 | 王云飞 | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
5 rows in set (0.00 sec)
五、使用通配符的查询
在MySQL中,使用通配符可实现模糊查询,通配符有两个:
1、百分号(%):表示在该位置可匹配任意多个字符
2、下划线(_):表示在该位置必须匹配一个字符
如果使用了通配符,必须使用LIKE运算符,命令如下:
mysql> select * from emp
where e_name like '张%'; --查询姓张的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 1 | 张平 | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | 人事部 |
| 2 | 张静静 | 1994-10-03 00:00:00 | 13803732222 | 4500.00 | 人事部 |
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp
where e_name like '张_'; --查询姓张并且名字只包含两个字的员工
+------+--------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+--------+---------------------+-------------+---------+------------+
| 1 | 张平 | 1990-01-23 00:00:00 | 13607589999 | 5000.00 | 人事部 |
+------+--------+---------------------+-------------+---------+------------+
1 row in set (0.00 sec)
mysql> select * from emp
where e_name like '%刚'; --查询名字的最后一个字是【刚】的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
1 row in set (0.01 sec)
mysql> select * from emp where e_name like '%贝%'; --查询名字中包含【贝】字的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 6 | 刘小贝 | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | 销售部 |
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
2 rows in set (0.00 sec)
说明:如果使用了通配符,并且要使用索引,则通配符不能放在字符串的开头。即:
e_name like ‘张%’:可以使用索引
e_name like ‘%张’:无法使用索引
六、使用order by字句对查询结果排序,命令如下:
mysql> select *
from emp where e_name like '刘%' order by department desc;
--查询结果按department字段降序排列
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 6 | 刘小贝 | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | 销售部 |
| 9 | 刘达龙 | 1989-06-06 00:00:00 | 13703736699 | 5900.00 | 财务部 |
| 10 | 刘胖胖 | 1992-11-03 00:00:00 | 13503736622 | 6800.00 | 财务部 |
| 3 | 刘鹏飞 | 1989-10-15 00:00:00 | 13903735874 | 5800.00 | 人事部 |
+------+-----------+---------------------+-------------+---------+------------+
4 rows in set (0.00 sec)
mysql> select e_name,department,birth,salary
from emp order by department,birth desc;
--查询结果先按department升序排列,部门相同的员工再按birth降序排列
+-----------+------------+---------------------+---------+
| e_name | department | birth | salary |
+-----------+------------+---------------------+---------+
| 张静静 | 人事部 | 1994-10-03 00:00:00 | 4500.00 |
| 张平 | 人事部 | 1990-01-23 00:00:00 | 5000.00 |
| 刘鹏飞 | 人事部 | 1989-10-15 00:00:00 | 5800.00 |
| 刘胖胖 | 财务部 | 1992-11-03 00:00:00 | 6800.00 |
| 刘达龙 | 财务部 | 1989-06-06 00:00:00 | 5900.00 |
| 刘小贝 | 销售部 | 1997-12-05 00:00:00 | 6700.00 |
| 王晓云 | 销售部 | 1992-03-30 00:00:00 | 6600.00 |
| 张红刚 | 销售部 | 1989-03-25 00:00:00 | 8000.00 |
| 王云飞 | 销售部 | 1984-03-12 00:00:00 | 9200.00 |
| 王贝贝 | 销售部 | 1979-01-30 00:00:00 | 9500.00 |
+-----------+------------+---------------------+---------+
10 rows in set (0.00 sec)
七、使用聚合函数对查询结果进行汇总
可以利用聚合函数对查询结果进行统计和汇总,常用的聚合函数有:
(1)count(*)函数:用来统计查询结果的行数,参数一般使用(*)。
(2)sum(数值型字段)函数:用来对一个数值型字段进行求和。
(3)avg(数值型字段)函数:用来对一个数值型字段进行求平均值。
(4)max(字段)函数:返回字段中的最大值。
(5)min(字段)函数:返回字段中的最小值。
代码如下:
mysql> select '销售部' as 部门,count(*) as 员工人数 from emp
where department='销售部'; --统计销售部的员工人数
+-----------+--------------+
| 部门 | 员工人数 |
+-----------+--------------+
| 销售部 | 5 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select '销售部' as 部门,sum(salary) as 工资总额 from emp
where department='销售部';--统计销售部的工资总额
+-----------+--------------+
| 部门 | 工资总额 |
+-----------+--------------+
| 销售部 | 40000.00 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select '销售部' as 部门,avg(salary) as 平均工资 from emp
where department='销售部';--统计销售部的平均工资
+-----------+--------------+
| 部门 | 平均工资 |
+-----------+--------------+
| 销售部 | 8000.000000 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select max(salary) as 最高工资 from emp; --查询最高工资
+--------------+
| 最高工资 |
+--------------+
| 9500.00 |
+--------------+
1 row in set (0.00 sec)
mysql> select min(salary) as 最低工资 from emp; --查询最低工资
+--------------+
| 最低工资 |
+--------------+
| 4500.00 |
+--------------+
1 row in set (0.00 sec)
说明:如果使用了聚合函数,则select一般不放任何字段名。
八、使用group by进行分组查询
1、聚合函数是对整个查询结果进行统计和汇总,而使用group by可以对查询结果进行分组,然后再使用聚合函数对每一组的数据分别进行统计和汇总。
2、where和having的区别:如果在分组查询中使用了where,则先用where对查询结果进行筛选,然后再进行分组。如果使用了having,则对【分组】进行筛选。
3、group by后面的字段一般要放到select后面,使分组字段出现在查询结果中。
mysql> select department as 部门,count(*) as 员工人数,avg(salary) as 平均工资
from emp group by deepartment; --统计每个部门的人数和平均工资
+-----------+--------------+--------------+
| 部门 | 员工人数 | 平均工资 |
+-----------+--------------+--------------+
| 人事部 | 3 | 5100.000000 |
| 财务部 | 2 | 6350.000000 |
| 销售部 | 5 | 8000.000000 |
+-----------+--------------+--------------+
3 rows in set (0.00 sec)
mysql> select department as 部门,count(*) as 员工人数 from emp
where salary>=5500 group by department; --统计每个部门工资超过5500的人数
+-----------+--------------+
| 部门 | 员工人数 |
+-----------+--------------+
| 人事部 | 1 |
| 财务部 | 2 |
| 销售部 | 5 |
+-----------+--------------+
3 rows in set (0.01 sec)
mysql> select department as 部门,count(*) as 员工人数,avg(salary) as 平均工资
from emp group by department having avg(salary)>=6000;
--统计每个部门的人数和平均工资,并且筛选出平均工资高于6000的部门
+-----------+--------------+--------------+
| 部门 | 员工人数 | 平均工资 |
+-----------+--------------+--------------+
| 财务部 | 2 | 6350.000000 |
| 销售部 | 5 | 8000.000000 |
+-----------+--------------+--------------+
2 rows in set (0.00 sec)
九、使用limit分页功能
格式:limit [m,]n
说明:第一个参数m可以省略,代表起始索引,若不指定m,则m默认为0,代表从第一条记录开始。第二个参数n不能省略,表示从第m+1个记录开始取n条记录。
代码如下:
mysql> select * from emp order by salary desc limit 3; --选取工资最高的3个员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 7 | 王贝贝 | 1979-01-30 00:00:00 | 13503735888 | 9500.00 | 销售部 |
| 8 | 王云飞 | 1984-03-12 00:00:00 | 13603734848 | 9200.00 | 销售部 |
| 5 | 张红刚 | 1989-03-25 00:00:00 | 13603736446 | 8000.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
3 rows in set (0.00 sec)
mysql> select * from emp order by birth desc limit 1;
--选取年龄最小(birth最大表示年龄最小)的员工
+------+-----------+---------------------+-------------+---------+------------+
| e_id | e_name | birth | phone | salary | department |
+------+-----------+---------------------+-------------+---------+------------+
| 6 | 刘小贝 | 1997-12-05 00:00:00 | 13803731234 | 6700.00 | 销售部 |
+------+-----------+---------------------+-------------+---------+------------+
1 row in set (0.00 sec)