Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Cách lấy mức lương thứ hai cao nhất từ bảng MySQL

Dùng bảng Salary:

mysql> Select * from Salary;
+--------+--------+
| Name   | Salary |
+--------+--------+
| Gaurav |  50000 |
| Rahul  |  40000 |
| Ram    |  45000 |
| Raman  |  45000 |
+--------+--------+
4 rows in set (0.00 sec)

mysql> Select * from salary12345 order by salary DESC limit 1 offset 1;
+-------+--------+
| name  | Salary |
+-------+--------+
| Raman |  45000 |
+-------+--------+
1 row in set (0.00 sec)

Sắp xếp giảm dần theo lương, bỏ qua 1 hàng đầu (OFFSET 1), lấy 1 hàng (LIMIT 1).