【问题标题】:Add serial number for each id in Postgres在 Postgres 中为每个 id 添加序列号
【发布时间】:2016-08-26 19:52:58
【问题描述】:

我必须在 postgres 中创建带有 serno、id、name、address 的表,并且 serno 应该是来自其他表的每个 id 的流水号。我有以下查询

CREATE TABLE Ids (SerNo integer, Id varchar(100),Name varchar(250),Address varchar(500));

INSERT INTO Ids (SerNo,Id,Name,Address) 
VALUES (rank() OVER(ORDER BY "Id"),
(SELECT distinct("Id") from   "Table2"),'John','US');

ERROR:  window functions are not allowed in VALUES
LINE 2: VALUES (rank() OVER(ORDER BY "Id"),

请指正

【问题讨论】:

    标签: postgresql insert create-table


    【解决方案1】:

    使用序列:

    create table Ids (SerNo serial...
    
    insert into Ids (Id,Name,Address) 
    select distinct Id, 'John', 'US'
    from Table2;
    

    https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL

    【讨论】:

    • 知道了!!谢谢。
    猜你喜欢
    • 2014-02-19
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2015-06-03
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多