两张桌子。一个包含员工ID,姓名,名称.. 第二个表包含id,month,salary。
我需要获得最高工资的输出,包括月份和名称,员工的指定。
答案 0 :(得分:2)
考虑如下表格;
mysql> select * from T1;
+----+------+-------------+
| id | name | Designation |
+----+------+-------------+
| 1 | a | man |
| 2 | b | assis |
| 3 | c | serv |
+----+------+-------------+
和
mysql> select * from T2;
+----+-------+--------+
| id | month | salary |
+----+-------+--------+
| 1 | jan | 3000 |
| 2 | jan | 2000 |
| 3 | jan | 1000 |
| 1 | Feb | 4000 |
| 2 | Feb | 3000 |
| 3 | Feb | 2000 |
+----+-------+--------+
您的要求: 获得"最高薪水与月份和名称,员工的指定"
查询:
select name,Designation,max(salary) from T1 join T2 using (id)
group by name;
结果:
+------+-------------+-------------+
| name | Designation | max(salary) |
+------+-------------+-------------+
| a | man | 4000 |
| b | assis | 3000 |
| c | serv | 2000 |
+------+-------------+-------------+
如果您想要每个月每位员工的最高薪水:
查询:
select name,Designation,month,max(salary) from T1 join T2 using (id)
group by name,month;
结果:
+------+-------------+-------+-------------+
| name | Designation | month | max(salary) |
+------+-------------+-------+-------------+
| a | man | Feb | 4000 |
| a | man | jan | 3000 |
| b | assis | Feb | 3000 |
| b | assis | jan | 2000 |
| c | serv | Feb | 2000 |
| c | serv | jan | 1000 |
+------+-------------+-------+-------------+
希望这会有所帮助!!
答案 1 :(得分:0)
试试这个: -
SELECT x1.month,e1.name,e1.designation,x1.salary FROM employee e1 JOIN (SELECT max(salary)
as salary,month,name,employeeid FROM salary GROUP BY employeeid)x1
ON x1.salary = e1.salary AND e1.id = x1.employeeid
答案 2 :(得分:0)
go list -m all