在普适性PSQL中(特别是(,使用sql select语句,如何从没有数字ID的表中随机选择一行?
这似乎对我使用PSQL v11.30有效。
create table chartest (f1 char (10), f2 char(10), f3 char(10));
insert into chartest values ('a','a','a');
insert into chartest values ('b','b','b');
insert into chartest values ('c','c','c');
insert into chartest values ('d','d','d');
insert into chartest values ('e','e','e');
insert into chartest values ('f','f','f');
select top 1 * from chartest order by newid()
我跑了几次,得到了:
<<<<<<<<<<<<<<<<<<<<<<<<
f1 f2 f3
========== ========== ==========
e e e
1 row was affected.
>>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<<<<<<<<<<<<
f1 f2 f3
========== ========== ==========
c c c
1 row was affected.
>>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<<<<<<<<<<<<
f1 f2 f3
========== ========== ==========
b b b
1 row was affected.
>>>>>>>>>>>>>>>>>>>>>>>>
<<<<<<<<<<<<<<<<<<<<<<<<
f1 f2 f3
========== ========== ==========
f f f
1 row was affected.
>>>>>>>>>>>>>>>>>>>>>>>>