Postgresql:将2列数据连接成1列

时间:2018-05-08 10:01:47

标签: postgresql psycopg2

我正在将数据从一个数据库迁移到另一个数据库(两个都是postgresql)。我想将源DB中的firstname和lastname列的值连接到目标DB的name列。当我尝试运行查询时,我收到一个错误:psycopg2.ProgrammingError: more than one row returned by a subquery used as an expression在SO中检查其他线程,我发现了这个concatenate。我在代码中添加了LIMIT 1,但它为我提供了表中现有名称的名字和姓氏。

我的代码:

cur_t.execute("""
                SELECT firstname, lastname
                FROM authors;
                """)

for row in cur_t:
    cur_p.execute("""
                     INSERT INTO lib_author (
                                                 created, modified, last_name,
                                                 first_name, country,
                                                 school_id, name)
                    VALUES (current_timestamp, current_timestamp, %s, %s, %s,
                            (SELECT id FROM ed_school WHERE name='My Test School'),
                            (SELECT CONCAT(first_name, ',', last_name) AS name FROM lib_author LIMIT 1)
                            )
                    """, (row['lastname'], row['firstname'], ''))

如何从源数据库中调用firstname和lastname列?

0 个答案:

没有答案