简体   繁体   English

SQL查询中where子句中n的作用是什么

[英]Whats the purpose of n in where clause in sql query

SELECT * 
FROM Employees Emp1 
WHERE (n) = ( SELECT COUNT(DISTINCT(Emp2.Salary)) 
              FROM Employees Emp2 
              WHERE Emp2.Salary >= Emp1.Salary )

I think what matters most is the subquery. 我认为最重要的是子查询。 It returns number of distinct salary that is greater than or equals to current Emp1.Salary . 它返回大于或等于当前Emp1.Salary This value returned is equal to the employee's salary rank. 返回的值等于员工的薪水等级。

Assume that you're the employee with third greatest salary, 10000. The subquery will count number of distinct salary that is greater than you which is 2 , plus one ( 2+1=3 ). 假设您是薪水第三高的员工,即10000。子查询将计算比您大的不同薪水数,即2 ,加一( 2+1=3 )。 Plus one counted from employee, including your self, having salary equals to 10000. This is because >= used in the WHERE clause. 再加上一个从雇员(包括您自己)算起的薪水等于10000。这是因为WHERE子句中使用了>=

Having said that, it makes perfect sense that the entire query select employee based on his salary rank. 话虽如此,整个查询都是根据员工的薪水等级来选择员工,这是完全有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM