我想在posgresql中生成唯一的“随机”数。
select s
from generate_series(1, 5) as s
order by random();
此查询将得出以下结果:
| s |
|---|
| 2 |
| 5 |
| 1 |
| 3 |
| 4 |
但是我想在其中附加一个id,如下所示:
| id | s |
|----|---|
| 1 | 2 |
| 2 | 5 |
| 3 | 1 |
| 4 | 3 |
| 5 | 4 |
答案 0 :(得分:4)
您可以使用ROW_NUMBER Function。
SELECT
ROW_NUMBER () OVER (ORDER BY random()) AS id,
s
FROM generate_series(1, 5) AS s;
答案 1 :(得分:1)
您可以尝试将自己的结果作为FROM(子查询)和ROW_NUMBER()OVER()
select t.s, ROW_NUMBER () OVER ()
from (
select s
from generate_series(1, 5) as s
order by random();
) t