MySQL选择两个具有重复条件的最小ID

时间:2018-12-07 13:42:29

标签: mysql

所以我有一个这样的饭厅

+---------+-------------+
| room_id | status      |
+---------+-------------+
| 1       | unavailable |
| 2       | available   |
| 3       | unavailable |
| 4       | available   |
| 5       | available   |
+---------+-------------+

如果我使用

SELECT * FROM room WHERE status='available'

输出当然是

+---------+-----------+
| room_id | status    |
+---------+-----------+
| 2       | available |
| 4       | available |
| 5       | available |
+---------+-----------+

但是我只希望输出这样的最小id中的两个

+---------+-----------+
| room_id | status    |
+---------+-----------+
| 2       | available |
| 4       | available |
+---------+-----------+

你们能帮我吗?谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作-使用按限制排序和限制

SELECT * FROM room WHERE status='available'
order by room_id asc
limit 2