時間:2024-03-08 08:50作者:下載吧人氣:22
在postgresql中,設置已存在的某列(num)值自增,可以用以下方法:
//將表tb按name排序,利用row_number() over()查詢序號并將該列命名為rownum,創建新表tb1并將結果保存到該表中
create table tb1 as (select *, row_number() over(order by name) as rownum from tb);
//根據兩張表共同的字段name,將tb1中rownum對應值更新到tb中num中
update tb set num=(select tb1.rownum from tb1 where tb.name = tb1.name);
//判斷表tb1的存在并刪除表
drop table if exists tb1;
網友評論