简体   繁体   English

将子查询结果设置为默认值

[英]Set subquery result as default value

I want to do the next:我想做下一个:

ALTER TABLE bets 
ADD COLUMN owner_id bigint NOT NULL DEFAULT (SELECT min(user_id) FROM users);

I'm confused postgres does not allow to save SELECT min(user_id) FROM users subquery's result to a variable and then reuse it.我很困惑 postgres 不允许将SELECT min(user_id) FROM users子查询的结果保存到变量然后重用它。

Any ideas how to set subquery result as default value?任何想法如何将子查询结果设置为默认值?

PS bets table is not empty PS bets表不为空

This seems to work for me:这似乎对我有用:

create function my_foobar_default(OUT id uuid)
as 'select id from foobar order by X desc limit 1'
language sql

alter table Y
add column if not exists SOMEVALUE uuid not null default my_foobar_default()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM