单表查询
创建数据表emp
mysql> CREATE TABLE emp (
-> empno int(4) NOT NULL,
-> ename varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
-> job varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
-> mgr int(4) NULL DEFAULT NULL,
-> hiredate date NOT NULL,
-> sai int(255) NOT NULL,
-> comm int(255) NULL DEFAULT NULL,
-> deptno int(2) NOT NULL,
-> PRIMARY KEY ( empno ) USING BTREE
-> ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Query OK, 0 rows affected (0.03 sec)
插入数据
mysql> INSERT INTO emp VALUES (1001, '甘宁', '文员', 1013, '2000-12-17', 8000, NULL, 20);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO emp VALUES (1002, '黛绮丝', '销售员', 1006, '2001-02-20', 16000, 3000, 30);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1003, '殷天正', '销售员', 1006, '2001-02-22', 12500, 5000, 30);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1004, '刘备', '经理', 1009, '2001-04-02', 29750, NULL, 20);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO emp VALUES (1005, '谢逊', '销售员', 1006, '2001-09-28', 12500, 14000, 30);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1006, '关羽', '经理', 1009, '2001-05-01', 28500, NULL, 30);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1007, '张飞', '经理', 1009, '2001-09-01', 24500, NULL, 10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1008, '诸葛亮', '分析师', 1004, '2007-04-19', 30000, NULL, 20);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1009, '曾阿牛', '董事长', NULL, '2001-11-17', 50000, NULL, 10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1010, '韦一笑', '销售员', 1006, '2001-09-08', 15000, 0, 30);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO emp VALUES (1011, '周泰', '文员', 1006, '2007-05-23', 11000, NULL, 20);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1012, '程普', '文员', 1006, '2001-12-03', 9500, NULL, 30);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1013, '庞统', '分析师', 1004, '2001-12-03', 30000, NULL, 20);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO emp VALUES (1014, '黄盖', '文员', 1007, '2002-01-23', 13000, NULL, 10);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO emp VALUES (1015, '张三', '保洁员', 1001, '2013-05-01', 80000, 50000, 50);
Query OK, 1 row affected (0.00 sec)
-- 1. 查询出部门编号为30的所有员工
mysql> select * from emp where deptno=30;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
6 rows in set (0.00 sec)
-- 2. 所有销售员的姓名、编号和部门编号。
mysql> select ename,empno,deptno from emp;
+-----------+-------+--------+
| ename | empno | deptno |
+-----------+-------+--------+
| 甘宁 | 1001 | 20 |
| 黛绮丝 | 1002 | 30 |
| 殷天正 | 1003 | 30 |
| 刘备 | 1004 | 20 |
| 谢逊 | 1005 | 30 |
| 关羽 | 1006 | 30 |
| 张飞 | 1007 | 10 |
| 诸葛亮 | 1008 | 20 |
| 曾阿牛 | 1009 | 10 |
| 韦一笑 | 1010 | 30 |
| 周泰 | 1011 | 20 |
| 程普 | 1012 | 30 |
| 庞统 | 1013 | 20 |
| 黄盖 | 1014 | 10 |
| 张三 | 1015 | 50 |
+-----------+-------+--------+
15 rows in set (0.00 sec)
-- 3. 找出奖金高于工资的员工。
mysql> select * from emp where comm > sai;
+-------+--------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+--------+-----------+------+------------+-------+-------+--------+
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
+-------+--------+-----------+------+------------+-------+-------+--------+
1 row in set (0.00 sec)
-- 4. 找出奖金高于工资60%的员工。
mysql> select * from emp where comm > sai * 0.6;
+-------+--------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+--------+-----------+------+------------+-------+-------+--------+
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+--------+-----------+------+------------+-------+-------+--------+
2 rows in set (0.00 sec)
-- 5. 找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料。
mysql> select * from emp where deptno=10 and job='经理' or deptno=20 and job='销售员';
+-------+--------+--------+------+------------+-------+------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+--------+--------+------+------------+-------+------+--------+
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
+-------+--------+--------+------+------------+-------+------+--------+
1 row in set (0.00 sec)
-- 6. 找出部门编号为10中所有经理,部门编号为20中所有销售员,还有即不是经理又不是销售员但其工 资大或等于20000的所有员工详细资料。
mysql> select * from emp where deptno=10 and job='经理' or deptno=20 and job='销售员' or sai>=20000 and job!='经理' and job!='销售员';
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
5 rows in set (0.00 sec)
-- 7. 无奖金或奖金低于1000的员工。
mysql> select * from emp where comm<1000 or comm is NULL;
+-------+-----------+-----------+------+------------+-------+------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
+-------+-----------+-----------+------+------------+-------+------+--------+
11 rows in set (0.00 sec)
-- 8. 查询名字由三个字组成的员工。 -- 注意:一个汉字占三个字节
mysql> select * from emp where ename like "___";
+-------+-----------+-----------+------+------------+-------+------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+------+--------+
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
+-------+-----------+-----------+------+------------+-------+------+--------+
5 rows in set (0.00 sec)
-- 9.查询2000年入职的员工。
mysql> select * from emp where hiredate like "2000%";
+-------+--------+--------+------+------------+------+------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+--------+--------+------+------------+------+------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
+-------+--------+--------+------+------------+------+------+--------+
1 row in set, 1 warning (0.00 sec)
-- 10. 查询所有员工详细信息,用编号升序排序
mysql> select * from emp order by empno asc;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.00 sec)
-- 11. 查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序
mysql> select * from emp order by sai desc,hiredate asc;
+-------+-----------+-----------+------+------------+-------+-------+--------+
| empno | ename | job | mgr | hiredate | sai | comm | deptno |
+-------+-----------+-----------+------+------------+-------+-------+--------+
| 1015 | 张三 | 保洁员 | 1001 | 2013-05-01 | 80000 | 50000 | 50 |
| 1009 | 曾阿牛 | 董事长 | NULL | 2001-11-17 | 50000 | NULL | 10 |
| 1013 | 庞统 | 分析师 | 1004 | 2001-12-03 | 30000 | NULL | 20 |
| 1008 | 诸葛亮 | 分析师 | 1004 | 2007-04-19 | 30000 | NULL | 20 |
| 1004 | 刘备 | 经理 | 1009 | 2001-04-02 | 29750 | NULL | 20 |
| 1006 | 关羽 | 经理 | 1009 | 2001-05-01 | 28500 | NULL | 30 |
| 1007 | 张飞 | 经理 | 1009 | 2001-09-01 | 24500 | NULL | 10 |
| 1002 | 黛绮丝 | 销售员 | 1006 | 2001-02-20 | 16000 | 3000 | 30 |
| 1010 | 韦一笑 | 销售员 | 1006 | 2001-09-08 | 15000 | 0 | 30 |
| 1014 | 黄盖 | 文员 | 1007 | 2002-01-23 | 13000 | NULL | 10 |
| 1003 | 殷天正 | 销售员 | 1006 | 2001-02-22 | 12500 | 5000 | 30 |
| 1005 | 谢逊 | 销售员 | 1006 | 2001-09-28 | 12500 | 14000 | 30 |
| 1011 | 周泰 | 文员 | 1006 | 2007-05-23 | 11000 | NULL | 20 |
| 1012 | 程普 | 文员 | 1006 | 2001-12-03 | 9500 | NULL | 30 |
| 1001 | 甘宁 | 文员 | 1013 | 2000-12-17 | 8000 | NULL | 20 |
+-------+-----------+-----------+------+------------+-------+-------+--------+
15 rows in set (0.00 sec)
-- 12.查询每个部门的平均工资
mysql> select avg(sai),deptno from emp group by deptno;
+------------+--------+
| avg(sai) | deptno |
+------------+--------+
| 29166.6667 | 10 |
| 21750.0000 | 20 |
| 15666.6667 | 30 |
| 80000.0000 | 50 |
+------------+--------+
4 rows in set (0.00 sec)
mysql>
-- 13.查询每个部门的雇员数量
mysql> select count(job),deptno from emp group by deptno;;
+------------+--------+
| count(job) | deptno |
+------------+--------+
| 3 | 10 |
| 5 | 20 |
| 6 | 30 |
| 1 | 50 |
+------------+--------+
4 rows in set (0.00 sec)
-- 14.查询每种工作的最高工资、最低工资、人数
mysql> select max(sai),min(sai),count(job),deptno from emp group by deptno;
+----------+----------+------------+--------+
| max(sai) | min(sai) | count(job) | deptno |
+----------+----------+------------+--------+
| 50000 | 13000 | 3 | 10 |
| 30000 | 8000 | 5 | 20 |
| 28500 | 9500 | 6 | 30 |
| 80000 | 80000 | 1 | 50 |
+----------+----------+------------+--------+
4 rows in set (0.00 sec)
多表连接查询
1、查询每个部门的所属员工
mysql> select * from dept3 d inner join emp3 e on d.deptno = e.dept_id;
+--------+-----------+-----+--------------+------+---------+
| deptno | name | eid | ename | age | dept_id |
+--------+-----------+-----+--------------+------+---------+
| 1001 | 研发部 | 1 | 乔峰 | 20 | 1001 |
| 1001 | 研发部 | 2 | 段誉 | 21 | 1001 |
| 1001 | 研发部 | 3 | 虚竹 | 23 | 1001 |
| 1001 | 研发部 | 4 | 阿紫 | 18 | 1001 |
| 1002 | 销售部 | 5 | 扫地僧 | 85 | 1002 |
| 1002 | 销售部 | 6 | 李秋水 | 33 | 1002 |
| 1002 | 销售部 | 7 | 鸠摩智 | 50 | 1002 |
| 1003 | 财务部 | 8 | 天山童姥 | 60 | 1003 |
| 1003 | 财务部 | 9 | 慕容博 | 58 | 1003 |
+--------+-----------+-----+--------------+------+---------+
9 rows in set (0.01 sec)
mysql> select * from dept3 d,emp3 e where d.deptno = e.dept_id;
+--------+-----------+-----+--------------+------+---------+
| deptno | name | eid | ename | age | dept_id |
+--------+-----------+-----+--------------+------+---------+
| 1001 | 研发部 | 1 | 乔峰 | 20 | 1001 |
| 1001 | 研发部 | 2 | 段誉 | 21 | 1001 |
| 1001 | 研发部 | 3 | 虚竹 | 23 | 1001 |
| 1001 | 研发部 | 4 | 阿紫 | 18 | 1001 |
| 1002 | 销售部 | 5 | 扫地僧 | 85 | 1002 |
| 1002 | 销售部 | 6 | 李秋水 | 33 | 1002 |
| 1002 | 销售部 | 7 | 鸠摩智 | 50 | 1002 |
| 1003 | 财务部 | 8 | 天山童姥 | 60 | 1003 |
| 1003 | 财务部 | 9 | 慕容博 | 58 | 1003 |
+--------+-----------+-----+--------------+------+---------+
9 rows in set (0.00 sec)
2、查询研发部门的所属员工
mysql> select * from dept3 d inner join emp3 e on d.deptno = e.dept_id and name='研发部';
+--------+-----------+-----+--------+------+---------+
| deptno | name | eid | ename | age | dept_id |
+--------+-----------+-----+--------+------+---------+
| 1001 | 研发部 | 1 | 乔峰 | 20 | 1001 |
| 1001 | 研发部 | 2 | 段誉 | 21 | 1001 |
| 1001 | 研发部 | 3 | 虚竹 | 23 | 1001 |
| 1001 | 研发部 | 4 | 阿紫 | 18 | 1001 |
+--------+-----------+-----+--------+------+---------+
4 rows in set (0.00 sec)
3、查询研发部和销售部的所属员工
mysql> select * from dept3 d inner join emp3 e on d.deptno = e.dept_id and name in ('研发部','销售部');
+--------+-----------+-----+-----------+------+---------+
| deptno | name | eid | ename | age | dept_id |
+--------+-----------+-----+-----------+------+---------+
| 1001 | 研发部 | 1 | 乔峰 | 20 | 1001 |
| 1001 | 研发部 | 2 | 段誉 | 21 | 1001 |
| 1001 | 研发部 | 3 | 虚竹 | 23 | 1001 |
| 1001 | 研发部 | 4 | 阿紫 | 18 | 1001 |
| 1002 | 销售部 | 5 | 扫地僧 | 85 | 1002 |
| 1002 | 销售部 | 6 | 李秋水 | 33 | 1002 |
| 1002 | 销售部 | 7 | 鸠摩智 | 50 | 1002 |
+--------+-----------+-----+-----------+------+---------+
7 rows in set (0.00 sec)
4、查询每个部门的员工数,并升序排序.文章来源:https://www.toymoban.com/news/detail-545553.html
mysql> select deptno,name,count(dept_id) from dept3 d left join emp3 e on d.deptno = e.dept_id group by deptno order by deptno desc;
+--------+-----------+----------------+
| deptno | name | count(dept_id) |
+--------+-----------+----------------+
| 1004 | 人事部 | 0 |
| 1003 | 财务部 | 2 |
| 1002 | 销售部 | 3 |
| 1001 | 研发部 | 4 |
+--------+-----------+----------------+
4 rows in set (0.00 sec)
5、查询人数大于等于3的部门,并按照人数降序排序文章来源地址https://www.toymoban.com/news/detail-545553.html
mysql> select deptno,name,count(*) from dept3 d left join emp3 e on d.deptno = e.dept_id group by d.deptno having count(*)>=3 order by counnt(*) desc;
+--------+-----------+----------+
| deptno | name | count(*) |
+--------+-----------+----------+
| 1001 | 研发部 | 4 |
| 1002 | 销售部 | 3 |
+--------+-----------+----------+
2 rows in set (0.00 sec)
到了这里,关于MySQL单表查询与多表连接查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!