我的查询如下所列
UPDATE tname
SET cname1 = TRIM(TRAILING 'xxx' from l_name),
cname2 = f_name,
cname3 = TRIM(TRAILING 'xxx' from l_name)
where tname.last_name ilike '%xxxyyy%' and cid in (zzzz);
如何在Postgres中使用CTE重写此内容?这样我就不会在查询中重复使用两次修整功能?
答案 0 :(得分:0)
尝试如下
with cte as
(
select TRIM(TRAILING 'xxx' from l_name) as n1,
f_name
from tname where tname.last_name like '%xxxyyy%' and cid in (zzzz)
) update tname
set cname1=n1,
cname2=cte.f_name,
cname3=n1
from cte
where t1.f_name=cte.f_name
答案 1 :(得分:0)
我不用担心。但是,如果您有主键,则可以使用:
UPDATE tname
SET cname1 = tt.trimmed_l_name,
cname2 = tname.f_name,
cname3 = tt.trimmed_l_name
FROM (SELECT tn.*, TRIM(TRAILING 'xxx' FROM l_name) as trimmed_l_name
FROM tname tn
) tt
ON tname.pk = tn.pk
WHERE tname.last_name ilike '%xxxyyy%' and tname.cid in (zzzz);