时间:2024-03-08 08:50作者:下载吧人气:27
在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;
网友评论