【发布时间】:2022-01-21 10:15:17
【问题描述】:
我有这样的数据
id userid phone
1 1000 11111
2 1001 22222
3 1000 33333
4 1000 44444
5 1001 55555
6 1000 66666
列电话是唯一的
我期待以下结果
position phone
3 44444
我尝试了 row_number 但它不起作用。
select *,row_number() over
(partition by userid order by id asc) as position
from table
where phone=44444
这种方式位置总是1
【问题讨论】:
-
你对
id的值感兴趣吗?您的语句中没有任何排序,因此无法保证返回的顺序。 -
row_number在应用 WHERE 之后应用,因此您计算一行行集的位置。在 CTE 中使用枚举并在外部查询中使用过滤。 -
我期待以下结果 对于指定的
order by id asc,phone=44444的位置必须是 4,而不是 3...请仔细解释。 -
akira ,抱歉解释不好。我的意思是根据用户ID =手机用户ID的行列表询问计算位置。
-
select userid from table where phone=44444将给出 1000,然后select * from table where userid=1000将给出 ``` 1 1000 11111 3 1000 33333 4 1000 44444 6 1000 66666 ``` 表示 44444 行来第三
标签: mysql mariadb position row