Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我正在学习数据库,并且对查询有疑问:

如何锻炼平均工资,包括总工资和工资差异?

我的表如下:

Staff: staffNo(PK), staffSalary


我写的查询是

SELECT staffNo, staffSalary,staffSalary - (
SELECT AVG( staffSalary )
FROM Staff ) AS salDiff
FROM Staff
WHERE staffSalary > (
SELECT AVG( staffSalary )
FROM Staff )


该查询仅给我平均工资差异和员工实际工资,我如何才能找到我所拥有的每一列数据的实际平均工资?
在此示例中还可以使用联接吗?

最佳答案

假设每个员工有一行,这是我认为您要的(对于所有高于平均水平的员工):

select staffNo,
(Select avg(staffsalary) from staff) as avgSalary,
(select staffSalary from staff where staffNo = S.StaffNo) as Salary,
(Select staffSalary - (select avg(staffSalary) from staff) from staff where staffNo = S.staffNo ) as salDiff
from staff S
where staffSalary > (Select avg(staffSalary) from staff)


不知道为什么需要加入...除非您想查看所有用户的薪水,在这种情况下,您只需删除where子句,它将显示所有工作人员。

关于mysql - SQL中的员工工资,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20939995/

10-17 03:08