没有使用限制和子查询,无法从emp表中找到第二高薪

时间:2015-08-18 10:10:34

标签: mysql

@XmlRootElement(name = "RequestS")
public class RequestS {

    @Valid
    protected Language language;
    @XmlElement(type = String.class)
    @XmlJavaTypeAdapter(Adapter1 .class)
    @XmlSchemaType(name = "dateTime")
    protected DateTime requestDateTime;
    @XmlElement(required = true)
    @NotNull
    @Valid
    protected Order order;

/**
 * Getters and Setters  
 */

2 个答案:

答案 0 :(得分:1)

这只是rownum的可视化。

create table tbl_emp
(   id int auto_increment primary key,
    salary int not null,
    key(salary) -- with few rows, not used, run thru EXPLAIN cmd to prove it
);
insert tbl_emp(salary) values (1),(2),(3),(4),(5);  -- affordable employees

set @rownum:=0;
select * from
(
    select @rownum:=@rownum+1 as rownum,id,salary
    from tbl_emp
    order by salary desc
    limit 2
) xxx
where rownum=2;

+--------+----+--------+
| rownum | id | salary |
+--------+----+--------+
|      2 |  4 |      4 |
+--------+----+--------+

答案 1 :(得分:0)

可能您可以使用ORDER BYLIMIT子句

执行以下操作
SELECT
salary as secondHighest_salary
FROM
tbl_emp 
WHERE dept_Id = '57'
and Date_of_joining between '2015-01-01 00:00:00' and '2015-01-01 23:59:59'
ORDER BY salary desc
LIMIT 1,2;