pger's recent timeline updates
pger

pger

V2EX member #651693, joined on 2023-09-28 21:14:33 +08:00
pger's recent replies
当然 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/
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2914 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 17ms · UTC 07:06 · PVG 15:06 · LAX 00:06 · JFK 03:06
♥ Do have faith in what you're doing.