小编dfg*_*dfg的帖子

如何在 postgres 的条件插入中使用插入值

insert into addresses(street, city, user_id)
select 
      'street1','LA', 2
where exists (select * from users where users.id = $$$$$ AND users.storeaddress='true'))
Run Code Online (Sandbox Code Playgroud)

仅当用户的 storeaddress 值为“true”时,我才想插入用户的地址。

我希望 sql 自动使用 2 代替 $$$$$。如何在sql中做到这一点?

如果我简单地用 user_id 代替 $$$$$ 我得到

HINT:  There is a column named "user_id" in table "addresses", but it cannot be referenced from this part of the query.
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-9.3

4
推荐指数
1
解决办法
5869
查看次数

标签 统计

postgresql ×1

postgresql-9.3 ×1