我有一张雇员和工资表,用这种方式定义:
"name" (type: VARCHAR)
"salary" (type: INTEGER)
我可以使用什么查询来获得此表中第二高的薪水?
答案 0 :(得分:42)
这是一个解释关系的人。
Name Salary
Jim 6
Foo 5
Bar 5
Steve 4
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees))
Result --> Bar 5, Foo 5
修改强> 我拿了Manoj的第二篇文章,调整它,使它更具人性化。对我来说 n-1 并不直观;但是,使用我想要的值,2 =第2,3 =第3等等。
/* looking for 2nd highest salary -- notice the '=2' */
SELECT name,salary FROM employees
WHERE salary = (SELECT DISTINCT(salary) FROM employees as e1
WHERE (SELECT COUNT(DISTINCT(salary))=2 FROM employees as e2
WHERE e1.salary <= e2.salary)) ORDER BY name
Result --> Bar 5, Foo 5
答案 1 :(得分:40)
第二高薪的直接答案
SELECT name, salary
FROM employees ORDER BY `employees`.`salary` DESC LIMIT 1 , 1
另一个有趣的解决方案
SELECT salary
FROM emp
WHERE salary = (SELECT DISTINCT(salary)
FROM emp as e1
WHERE (n) = (SELECT COUNT(DISTINCT(salary))
FROM emp as e2
WHERE e1.salary <= e2.salary))
答案 2 :(得分:13)
似乎我很晚才回答这个问题。这个衬垫如何获得相同的输出?
SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1,1 ;
答案 3 :(得分:9)
create table svalue (
name varchar(5),
value int
) engine = myisam;
insert into svalue value ('aaa',30),('bbb',10),('ccc',30),('ddd',20);
select * from svalue where value = (
select value
from svalue
group by value
order by value desc limit 1,1)
答案 4 :(得分:5)
FOR SECOND END LAST:
SELECT name, salary
FROM employee
ORDER BY salary DESC
LIMIT 1 , 1
至于第三次:
SELECT name, salary
FROM employee
ORDER BY salary DESC
LIMIT 2 , 1
答案 5 :(得分:3)
显示具有第二大标记值的记录:
SELECT username, mark
FROM tbl_one
WHERE mark = (
SELECT DISTINCT mark
FROM tbl_one
ORDER by mark desc
LIMIT 1,1
);
答案 6 :(得分:3)
简单的解决方案
SELECT * FROM TBLNAME ORDER BY COLNAME ASC LIMIT (n - x), 1
注意:n =列
中的记录总数 x = value 2nd, 3rd, 4th highest etc
e.g
//to find employee with 7th highest salary
n = 100
x = 7
SELECT * FROM tbl_employee ORDER BY salary ASC LIMIT 93, 1
希望这会有所帮助
答案 7 :(得分:3)
找到另一个有趣的解决方案
SELECT salary
FROM emp
WHERE salary = (SELECT DISTINCT(salary)
FROM emp as e1
WHERE (n) = (SELECT COUNT(DISTINCT(salary))
FROM emp as e2
WHERE e1.salary <= e2.salary))
对不起。忘了写。 n是你想要的第n个薪水。
答案 8 :(得分:3)
获得第二高的值:
SELECT `salary` FROM `employees` ORDER BY `salary` DESC LIMIT 1, 1;
答案 9 :(得分:3)
要获得第二高薪,只需使用以下查询
SELECT salary FROM employees
ORDER BY salary DESC LIMIT 1,1;
答案 10 :(得分:3)
您可以使用以下提到的查询
SELECT emp.name, emp.salary
FROM employees emp
WHERE 2 = (SELECT COUNT(DISTINCT salary)
FROM employees
WHERE emp.salary<=salary
);
您可以将2更改为所需的最高记录。
答案 11 :(得分:2)
SELECT name, salary
FROM employees
where
salary = (SELECT (salary) FROM employees GROUP BY salary DESC LIMIT 1,1)
答案 12 :(得分:2)
要获得* N *最高值,最好使用此解决方案:
SELECT * FROM `employees` WHERE salary =
(SELECT DISTINCT(salary) FROM `employees`
ORDER BY salary DESC LIMIT {N-1},1);
或者您可以尝试:
SELECT * FROM `employees` e1 WHERE
(N-1) = (SELECT COUNT(DISTINCT(salary))
FROM `employees` e2
WHERE e1.salary < e2.salary );
N = 2,第二高 N = 3表示第三高等等。
答案 13 :(得分:2)
获得第二高薪
select max(salary) from salary where salary not in (select top 1 salary from salary order by salary desc)
获得第三高薪
select max(salary) from salary where salary not in (select top 2 salary from salary order by salary desc)
依旧......
答案 14 :(得分:2)
简单的解决方案如下面的查询:
select max(salary) as salary from employees where salary<(select max(salary) from employees);
答案 15 :(得分:1)
SELECT MAX(salary) salary
FROM tbl
WHERE salary <
(SELECT MAX(salary)
FROM tbl);
答案 16 :(得分:1)
SELECT DISTINCT Salary
FROM emp
ORDER BY salary DESC
LIMIT 1 , 1
此查询也会给出重复记录的第二高薪。
答案 17 :(得分:1)
尝试这个获得第n个最高工资
我在发布之前试过这个&amp;它工作正常
例如。找到第10个最高薪水替换限制9,1 ;
mysql> select name,salary from emp group by salary desc limit n-1,1;
答案 18 :(得分:1)
试试这个:
SELECT DISTINCT(`salary`)
FROM `employee`
ORDER BY `salary` DEC
LIMIT 1,1
答案 19 :(得分:0)
with alias as
(
select name,salary,row_number() over(order by salary desc ) as rn from employees
)
select name,salary from alias where rn=n--n being the nth highest salary
答案 20 :(得分:0)
SELECT username, salary
FROM tblname
GROUP by salary
ORDER by salary desc
LIMIT 0,1 ;
答案 21 :(得分:0)
SELECT name, salary
FROM EMPLOYEES
WHERE salary = (
SELECT DISTINCT salary
FROM EMPLOYEES
ORDER BY salary DESC
LIMIT 1 , 1 )
答案 22 :(得分:0)
SELECT name, salary
FROM employees
order by salary desc limit 1,1
这个查询应该可以完成你的工作。
首先,我们以递减的方式对表格进行排序,以便薪水最高的人位于顶部,第二个位于第二位置。现在limit a,b
表示跳过起始a
元素,然后打印下一个b
元素。所以在这种情况下你应该使用limit 1,1
。
希望这有帮助。
答案 23 :(得分:0)
SELECT name,salary FROM employee
WHERE salary = (SELECT DISTINCT(salary) FROM employee ORDER BY salary DESC LIMIT 1,1) ORDER BY name
答案 24 :(得分:0)
SELECT SALARY
FROM (SELECT *
FROM EMPLOYEE
ORDER BY SALARY
DESC LIMIT ***2***) AS TOP_SALARY
ORDER BY SALARY ASC
LIMIT 1
答案 25 :(得分:0)
SELECT MIN(id) as id FROM students where id>(SELECT MIN(id) FROM students);
答案 26 :(得分:-1)
试试这个:
package nestedcontrollerexample;
import javafx.beans.binding.Bindings;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
public class PersonListCellFactory implements Callback<ListView<Person>, ListCell<Person>> {
@Override
public ListCell<Person> call(ListView<Person> param) {
return new ListCell<Person>() {
@Override
public void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
textProperty().unbind();
if (person != null) {
textProperty().bind(Bindings.format("%s %s", person.firstNameProperty(), person.lastNameProperty()));
}
}
};
}
}