[英]How to use multiple order by in a sql query with limit keyword?
I want to get nth highest salary in a table by first limiting result set to n top salaries first and then to reverse the result set and limiting it to 1. 我想通过首先将结果集限制为n个最高薪水,然后反转结果集并将其限制为1,来获得表中的n个最高薪水。
I have used this query which is resulting into an error:- 我使用了此查询,这会导致错误:-
select *
from salaries
where emp_no=(select * from salaries order by salary desc limit 2) order by salary asc limit 1;
The error states that subquery return more than one row. 该错误表明子查询返回多行。
If you want second highest value you could use: 如果要第二高的值,可以使用:
SELECT *
FROM salaries
ORDER BY salary DESC
LIMIT 1,1;
Here's one way: 这是一种方法:
SELECT s.* FROM
(SELECT * FROM `salaries` ORDER BY salary DESC LIMIT 2) s
ORDER BY s.salary ASC LIMIT 1;
Never use SELECT *
, see http://www.parseerror.com/blog/select-*-is-evil 请勿使用
SELECT *
,请参阅http://www.parseerror.com/blog/select-*-is-evil
select * from salaries
order by salary
desc limit 1 offset 4;
https://www.db-fiddle.com/f/2itHSFs2enyNpJ3MK6Nxcz/0 https://www.db-fiddle.com/f/2itHSFs2enyNpJ3MK6Nxcz/0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.