hc学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › zhangjiafu › 日志

根据部门表和人员表,完成如下查询

2021-01-08 09:36:29 查看(332) 回复(0)

建表sql:


-- ----------------------------
-- Table structure for sg_dept
-- ----------------------------
DROP TABLE IF EXISTS `sg_dept`;
CREATE TABLE `sg_dept` (
  `deptno` int(11) NOT NULL,
  `dname` varchar(50) DEFAULT NULL,
  `loc` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`deptno`)
);

-- ----------------------------
-- Records of sg_dept
-- ----------------------------
INSERT INTO `sg_dept` VALUES ('10', '教研部', '北京');
INSERT INTO `sg_dept` VALUES ('20', '学工部', '上海');
INSERT INTO `sg_dept` VALUES ('30', '销售部', '广州');
INSERT INTO `sg_dept` VALUES ('40', '财务部', '武汉');
-- ----------------------------
-- Table structure for sg_emp
-- ----------------------------
DROP TABLE IF EXISTS `sg_emp`;
CREATE TABLE `sg_emp` (
  `empno` int(11) NOT NULL,
  `ename` varchar(50) DEFAULT NULL,
  `job` varchar(50) DEFAULT NULL,
  `mgr` int(11) DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` decimal(7,2) DEFAULT NULL,
  `COMM` decimal(7,2) DEFAULT NULL,
  `deptno` int(11) DEFAULT NULL,
  PRIMARY KEY (`empno`),
  KEY `fk_emp` (`mgr`),
  CONSTRAINT `fk_emp` FOREIGN KEY (`mgr`) REFERENCES `sg_emp` (`empno`)
);

-- ----------------------------
-- Records of emp
-- ----------------------------
INSERT INTO `sg_emp` VALUES ('1009', '董先生', '董事长', null, '2001-11-17', '50000.00', null, '10');
INSERT INTO `sg_emp` VALUES ('1004', '刘备', '经理', '1009', '2001-04-02', '29750.00', null, '20');
INSERT INTO `sg_emp` VALUES ('1008', '诸葛亮', '分析师', '1004', '2007-04-19', '30000.00', null, '20');
INSERT INTO `sg_emp` VALUES ('1013', '庞统', '分析师', '1004', '2001-12-03', '30000.00', null, '20');
INSERT INTO `sg_emp` VALUES ('1001', '甘宁', '文员', '1013', '2000-12-17', '8000.00', null, '20');
INSERT INTO `sg_emp` VALUES ('1006', '关羽', '经理', '1009', '2001-05-01', '28500.00', null, '30');
INSERT INTO `sg_emp` VALUES ('1002', '黛绮丝', '销售员', '1006', '2001-02-20', '16000.00', '3000.00', '30');
INSERT INTO `sg_emp` VALUES ('1003', '殷天正', '销售员', '1006', '2001-02-22', '12500.00', '5000.00', '30');
INSERT INTO `sg_emp` VALUES ('1005', '谢逊', '销售员', '1006', '2001-09-28', '12500.00', '14000.00', '30');
INSERT INTO `sg_emp` VALUES ('1007', '张飞', '经理', '1009', '2001-09-01', '24500.00', null, '10');
INSERT INTO `sg_emp` VALUES ('1010', '韦一笑', '销售员', '1006', '2001-09-08', '15000.00', '0.00', '30');
INSERT INTO `sg_emp` VALUES ('1011', '周泰', '文员', '1008', '2007-05-23', '11000.00', null, '20');
INSERT INTO `sg_emp` VALUES ('1012', '程普', '文员', '1006', '2001-12-03', '9500.00', null, '30');
INSERT INTO `sg_emp` VALUES ('1014', '黄盖', '文员', '1007', '2002-01-23', '13000.00', null, '10');


问题:


  1. 列出薪金比关羽高的所有员工
  2. 列出所有员工的姓名及其直接上级的姓名
  3. 列出受雇日期早于直接上级的所有员工的编号、姓名、部门名称
  4. 列出部门名称和这些部门的员工姓名,同时列出那些没有员工的部门
  5. 列出部门的部门名称,以及部门中文员的人数,以及部门中所有文员的姓名


-- 1

select ename from sg_emp where sal>( select sal from sg_emp where ename='关羽')

-- 2

select mgr,group_concat(ename) from sg_emp group by mgr order by mgr desc


select ename ,c.*  from (select mgr,group_concat(ename) from sg_emp group by mgr order by mgr desc) c,sg_emp where c.mgr=sg_emp.empno

-- 3

select a.ename ,a.empno, a.deptno from sg_emp a where a.hiredate<(select hiredate from sg_emp  where sg_emp.empno=a.mgr);

select dname ,c.* from (select a.ename ,a.empno, a.deptno from sg_emp a where a.hiredate<(select hiredate from sg_emp  where sg_emp.empno=a.mgr)) c,sg_dept where c.deptno=sg_dept.deptno

-- 4

select sg_dept.*,sg_emp.* from sg_dept left join sg_emp on sg_dept.deptno=sg_emp.deptno

-- 5

select e.ename,e.deptno from sg_emp e, sg_dept d where e.deptno=d.deptno and e.job='文员'

select count(deptno),deptno from sg_emp group by deptno

select e.deptno,d.dname,count(e.deptno)from sg_emp e, sg_dept d

  where e.deptno=d.deptno and e.job='文员' group by e.deptno


select e.ename , e.dname ,d.* from (select e.ename,e.deptno ,d.dname from sg_emp e, sg_dept d where e.deptno=d.deptno and e.job='文员') e left join (select count(deptno),deptno from sg_emp group by deptno

) d on e.deptno=d.deptno

评论 (0 个评论)

facelist

全部作者的其他最新日志



站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

f1208.com 备案号:京ICP备14030918号-1

返回顶部