gpt4 book ai didi

sql - 如何改进 postgresql 查询?

转载 作者:行者123 更新时间:2023-11-29 13:02:25 24 4
gpt4 key购买 nike

有人可以告诉我如何改进下面的查询并使它成为一个语句而不是两个语句吗?谢谢

CREATE LOCAL TEMP TABLE tmpdetail2
WITH (OIDS)
ON COMMIT DROP as
select d2.detailid, d2.objid, d2.p
from _detail d2
where d2.detailid in (19, 106);

select distinct d.detailid, d.p, d.pval, d.objid
from _detail d
left join tmpdetail2 d2
on d.objid = d2.objid
where d2.objid is null
and d.p not in(select p from tmpdetail2)
order by p asc, d.detailid asc;

最佳答案

使用 common table expression :

with tmpdetail2 as (
select d2.detailid, d2.objid, d2.p
from _detail d2
where d2.detailid in (19, 106)
)
select distinct d.detailid, d.p, d.pval, d.objid
from _detail d
left join tmpdetail2 d2
on d.objid = d2.objid
where d2.objid is null
and d.p not in(select p from tmpdetail2)
order by p asc, d.detailid asc;

关于sql - 如何改进 postgresql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com