gpt4 book ai didi

mysql统计哪条记录是有序模式?

转载 作者:行者123 更新时间:2023-11-29 09:12:14 25 4
gpt4 key购买 nike

考虑这个表

id  -  name  -  age
1 jonny 12
2 al 16
3 lory 22

现在按年龄排序,我们有 Lory、Al、Jonny。

有没有一种快速的方法来选择列表中的号码 jonny?

考虑到这个顺序SELECT * FROM table ORDER BY Age DESC,jonny 是第三名。

类似于:从表 WHERE id = 1 中选择 *, * 排名(年龄)作为位置**

编辑:

我可以计算年龄>我的记录的记录:所以查询变成:

SELECT COUNT(id) FROM table WHERE age > (SELECT age FROM table WHERE id = 1) as t1

我已经接受了答案,但我不喜欢在 sql 查询中使用变量。

最佳答案

set @name = 'al';
select @name,count(*) + 1 as rank from
(select * from `table` where age >
(select age from `table` where name=@name) order by age desc)as tab;

关于mysql统计哪条记录是有序模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5276118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com