COUNT与RANK mysql

时间:2013-11-27 00:16:33

标签: mysql sql

从我的数据库中我想得到员工的姓氏,收入高于他的员工人数,然后按降序排列员工的工资;收入最高的员工为零。我是这样做的:

SELECT 0+COUNT(b.salary) rank
, a.lname
, a.salary
, COUNT(*) AS employee_count
FROM employee a LEFT OUTER JOIN employee b
ON a.salary<b.salary
GROUP BY a.salary, a.lname
ORDER BY salary DESC

结果显示如下:

enter image description here

到目前为止一切顺利。除此之外,博格不应该在那里。因为没有人比他赚得更多。现在发生这种情况是因为使用了LEFT OUTER JOIN。我尝试使用INNER JOIN,但它给出了语法错误。所以问题是如何让INNER JOIN与之合作呢?

1 个答案:

答案 0 :(得分:1)

如果您只想消除最高值:

SELECT *
FROM (    
    SELECT 0+COUNT(b.salary) rank
        , a.lname
        , a.salary
        , COUNT(*) AS employee_count
    FROM employee a 
    LEFT OUTER JOIN employee b
        ON a.salary < b.salary
    GROUP BY a.salary, a.lname
    ORDER BY salary DESC
) T1
WHERE T1.rank > 0