将子查询中的多个数据插入到Postgres SQL中的另一个表中 [英] Insert multiple data from subquery into another table in postgres sql

查看:480
本文介绍了将子查询中的多个数据插入到Postgres SQL中的另一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列的表alpha

I have table alpha with two columns

id   school_name

我还有另一个表beta,其中包含约600行数据,其后各列

I have another table beta which has around 600 rows of data with following columns

id  school_name    school_state school_city

现在我想从Beta中选择school_name并将其插入alpha

now i want to select school_name from beta and insert it into alpha

类似

insert into alpha (school_name) values(select school_name from beta )

但是对于所有数据,我都知道可以使用过程来完成,但pgsql不会支持程序不同于mysql,那么如何实现呢?

but for all data ,I know it can be done with procedures but pgsql doesn't support procedures unlike mysql ,so how to achieve it?

推荐答案

INSERT INTO ... SELECT 语法不使用 VALUES 子句。稍稍修正语法,查询将起作用:

The INSERT INTO ... SELECT syntax does not use a VALUES clause. Fix your syntax slightly, and your query should work:

INSERT INTO alpha (school_name)
SELECT school_name
FROM beta;

这篇关于将子查询中的多个数据插入到Postgres SQL中的另一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆