時(shí)間:2024-03-08 08:50作者:下載吧人氣:27
在postgresql中,設(shè)置已存在的某列(num)值自增,可以用以下方法:
//將表tb按name排序,利用row_number() over()查詢序號(hào)并將該列命名為rownum,創(chuàng)建新表tb1并將結(jié)果保存到該表中
create table tb1 as (select *, row_number() over(order by name) as rownum from tb);
//根據(jù)兩張表共同的字段name,將tb1中rownum對(duì)應(yīng)值更新到tb中num中
update tb set num=(select tb1.rownum from tb1 where tb.name = tb1.name);
//判斷表tb1的存在并刪除表
drop table if exists tb1;
盖楼回复X
(您的评论需要经过审核才能显示)
網(wǎng)友評(píng)論