SQL表
部门表
DROP TABLE IF EXISTS `demp`;
CREATE TABLE `demp` (
`id` int(11) DEFAULT NULL,
`depname` varchar(255) DEFAULT NULL,
`depnum` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
员工表
DROP TABLE IF EXISTS `stuff`;
CREATE TABLE `stuff` (
`id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`dempid` int(11) DEFAULT NULL,
`salary` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
工资等级表
DROP TABLE IF EXISTS `avgal`;
CREATE TABLE `avgal` (
`id` int(11) DEFAULT NULL,
`degreat` int(255) DEFAULT NULL,
`avgal` decimal(10,0) DEFAULT NULL,
`dvgal` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
添加数据
INSERT INTO `demp` VALUES ('1', 'IT部', '1');
INSERT INTO `demp` VALUES ('2', '业务部', '2');
INSERT INTO `demp` VALUES ('3', '财务部', '3');
INSERT INTO `demp` VALUES ('4', '行政部', '4');
INSERT INTO `stuff` VALUES ('1', 'yye', '1', '7800');
INSERT INTO `stuff` VALUES ('2', 'yangle', '1', '7900');
INSERT INTO `stuff` VALUES ('3', '张三', '2', '3500');
INSERT INTO `stuff` VALUES ('4', '李四', '2', '1400');
INSERT INTO `stuff` VALUES ('5', '王五', '3', '2500');
INSERT INTO `stuff` VALUES ('6', '赵六', '3', '5600');
INSERT INTO `stuff` VALUES ('7', '小娟', '4', '7999');
INSERT INTO `stuff` VALUES ('8', '小霞', '4', '6000');
INSERT INTO `avgal` VALUES ('1', '1', '500', '1500');
INSERT INTO `avgal` VALUES ('2', '2', '1501', '2500');
INSERT INTO `avgal` VALUES ('3', '3', '2501', '3500');
INSERT INTO `avgal` VALUES ('4', '4', '3501', '4500');
INSERT INTO `avgal` VALUES ('5', '5', '4501', '8000');
1、查询it部门最高的工资等级
SELECT avgal.degreat FROM avgal WHERE
(SELECT
MAX(stuff.salary) AS salary
FROM
stuff
WHERE
dempid = 1)
between avgal and dvgal
2、查询各部门最高的工资等级
SELECT degreat,depname,salary ,b.name FROM avgal a,
(SELECT depname,MAX(salary) AS salary,name from stuff,demp where
stuff.dempid=demp.id GROUP BY demp.id) b
WHERE b.salary BETWEEN avgal and dvgal