V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  pger  ›  全部回复第 1 页 / 共 1 页
回复总数  3
当然 UNION 也是一种选择:

insert into mytab(name) values('aaron') on conflict(name) do nothing returning id
UNION
select id from mytab where name = 'aaron';

参考:
UNION: 组合多个查询的结果集
https://www.rockdata.net/zh-cn/tutorial/dml-union/
同为程序员,我很理解你的这种简洁的癖好,和对极致性能的追求。

创建个 PL/pgSQL 函数,包装一下插入逻辑:
insert into mytab(name) values('aaron') on conflict(name) do nothing returning id;
如果返回的 id 值为 NULL ,再使用 SELECT 查询对应 name 的 id ,作为函数返回值;

参考:
创建 PL/pgSQL 函数:
https://www.rockdata.net/zh-cn/tutorial/plpgsql-create-function/

使用 INSERT ON CONFLICT 语句进行更新插入:
https://www.rockdata.net/zh-cn/tutorial/dml-upsert/
timestamp with time zone 跟你认为的类型是一样的。
可以参考下:
https://www.rockdata.net/zh-cn/tutorial/type-timestamp/
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5689 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 09:01 · PVG 17:01 · LAX 02:01 · JFK 05:01
Developed with CodeLauncher
♥ Do have faith in what you're doing.