[英]Sql Insert with dynamic values
I am using following query on postgres database: 我在postgres数据库上使用以下查询:
insert into user_settings (google_access_token, google_refresh_token)
select 'google_access_token', 'google_refresh_token' from user
where id = user_id
table user_settings has a field user_id
which corresponds to field named id
in user table. 表user_settings有一个字段user_id
,它对应于用户表中名为id
字段。 but this gives the error: 但这给出了错误:
column "user_id" does not exist
Hint: There is a column named "user_id" in table "user_settings",
but it cannot be referenced from this part of the query
I guess you need to do like this : 我想你需要这样做:
insert into user_settings (google_access_token, google_refresh_token)
select 'google_access_token',
'google_refresh_token'
from user
where id in (select user_id from user_settings )
you are taking a column in user_settings and inserting into that table with condition. 您正在user_settings中的一列并有条件地插入到该表中。 I think you have to use update statement because your condition is 我认为您必须使用update语句,因为您的条件是
user .id = user_settings .user_id 用户.id =用户设置.user_id
You can update a table with out NULLs even if you don't have matching values 即使您没有匹配的值,也可以用空值更新表
update user_settings
set
user_settings.google_access_token='google_access_token',
user_settings.google_refresh_token='google_refresh_token'
from user_settings
inner join user
on user_settings.user_id=user.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.