我有如下表记录: -
MY_TABLE
id | rating | description
1 | 0.0 | bed
2 | 1.0 | good
3 | 0.0 | bed
4 | 1.0 | good
5 | 0.0 | bed
6 | 0.0 | bed
7 | 0.0 | bed
现在我按评分缩短了这张表,我得到了以下结果。 (查询:SELECT * FROM my_table ORDER BY rating DESC)
结果表
id | rating | description
2 | 1.0 | good
4 | 1.0 | good
1 | 0.0 | bed
==> 3 | 0.0 | bed
5 | 0.0 | bed
6 | 0.0 | bed
7 | 0.0 | bed
现在我想要先前的id = 3记录。
预期结果
id | rating | description
1 | 0.0 | bed
我怎样才能实现它。在单个mysql查询中? 请帮帮我。
答案 0 :(得分:4)
使用此:
SELECT * FROM my_table ORDER BY rating DESC LIMIT 2,1
答案 1 :(得分:0)
试试这个
SELECT prev.id, rating, description
FROM my_table current
JOIN my_table prev
ON (prev.rating < current.rating)
OR (prev.rating = current.rating
AND (prev.description < current.description
OR (prev.description = current.description AND prev.id < current.id)))
WHERE current.id = @currentId
ORDER BY prev.rating desc, prev.description desc, prev.id desc
LIMIT 1
答案 2 :(得分:0)
试试这个以获得快速解决方案:
SELECT * FROM my_table WHERE rating ='0.0'且id&lt;'3'ORDER BY ID DESC LIMIT 1
这将选择ID低于指定评级为0.0的ID的行。这将在没有排序的情况下工作但是不会检索任何评级为1.0的行,即使它们是下一行。
答案 3 :(得分:0)
试试这个:
select * from my_table
where rating<(select max(rating) from my_table)
and
id<3
limit 1;