pger 最近的时间轴更新
pger

pger

V2EX 第 651693 号会员,加入于 2023-09-28 21:14:33 +08:00
pger 最近回复了
当然 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   ·   我们的愿景   ·   实用小工具   ·   778 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 10ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
Developed with CodeLauncher
♥ Do have faith in what you're doing.