gpt4 book ai didi

sql - 在 SQL 中运行数字

转载 作者:行者123 更新时间:2023-12-01 07:48:43 25 4
gpt4 key购买 nike

我有一个这样的 SQL 语句:

SELECT name FROM users WHERE deleted = 0;

如何在第一行创建一个带有运行编号的结果集?所以结果看起来像这样:
1 Name_1
2 Name_3
3 Name_12
4 Name_15
...

最佳答案

Oracle , SQL ServerPostreSQL 8.4 :

SELECT  ROW_NUMBER() OVER (ORDER BY name)
FROM users
WHERE deleted = 0

MySQL :
SELECT  @r := @r + 1 AS rn
FROM (
SELECT @r := 0
) vars, users
WHERE deleted = 0
ORDER BY
name

PostgreSQL 8.3 :
SELECT  num, (arr[num]).*
FROM (
SELECT generate_series(1, array_upper(arr, 1)) AS num, arr
FROM (
SELECT ARRAY
(
SELECT users
FROM users
ORDER BY
name
) AS arr
) q
) q2

关于sql - 在 SQL 中运行数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1701342/

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