Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何获取特定值在结果列表中的位置?_Mysql - Fatal编程技术网

Mysql 如何获取特定值在结果列表中的位置?

Mysql 如何获取特定值在结果列表中的位置?,mysql,Mysql,在这里,我得到了一份按类型分组的酒店列表,并按可用房间数排序 SELECT hotel_type, SUM(room_available) room_available FROM history WHERE hotel_name = 'anyHotel' GROUP BY hotel_type ORDER BY room_available DESC 我希望获得此列表中出现的hotel\u type='Classic'第一行的位置(行号) 如何实现这一点?如果您运行的是MySQL 8.0,您可

在这里,我得到了一份按类型分组的酒店列表,并按可用房间数排序

SELECT hotel_type, SUM(room_available) room_available FROM history WHERE hotel_name = 'anyHotel' GROUP BY hotel_type ORDER BY room_available DESC
我希望获得此列表中出现的
hotel\u type='Classic'
第一行的位置(行号)


如何实现这一点?

如果您运行的是MySQL 8.0,您可以使用
rank()
和子查询来实现这一点:

select *
from (
    select hotel_type, sum(room_available) room_available,
        rank() over(order by sum(room_available) desc) rn
    from history 
    where hotel_name = 'anyHotel' 
    group by hotel_type 
) t
where hotel_type = 'Classic'

非常感谢你!愿上帝保佑你和你的家人。