从UPDATE命令SQL引用外部派生表
我需要在UPDATE命令中从查询中引用cartProduct派生表。有人能解释一下我做错了什么,我怎样才能纠正它吗从UPDATE命令SQL引用外部派生表,sql,sql-update,derived-table,Sql,Sql Update,Derived Table,我需要在UPDATE命令中从查询中引用cartProduct派生表。有人能解释一下我做错了什么,我怎样才能纠正它吗 UPDATE memberships SET points = (points + COALESCE( (SELECT pts FROM ( SELECT pid0, SUM (S) as Pts FROM ( (SELECT pid0, SUM (pts1) as S FROM
UPDATE memberships SET points = (points + COALESCE(
(SELECT pts
FROM (
SELECT pid0, SUM (S) as Pts
FROM (
(SELECT pid0, SUM (pts1) as S
FROM
(SELECT DISTINCT pid0, pid0,pts1
FROM cartProduct
WHERE pid0 = i1) as pointsForPath
GROUP BY pid0)) as allPtsTbl
GROUP BY pid0) as temp
WHERE memberships.cid = 57010 AND memberships.pid = temp.pid0 ),
0))
FROM (
SELECT t0.pid as pid0,t1.pid as pid1,t1.invitedby as i1,t1.points as pts1, t2.pid as pid2,t2.invitedby as i2,t2.points as pts2,t3.pid as pid3,t3.invitedby as i3, t3.points as pts3
FROM memberships t0, memberships t1, memberships t2, memberships t3
WHERE t0.cid = t1.cid AND t1.cid = t2.cid AND t2.cid = t3.cid) as cartProduct;
这个查询必须做什么并不重要,因为这是一个复杂的查询,我想优化它。我唯一的问题是如何从更新子查询中引用cartProduct。
我要优化的查询是由C代码生成的。。。正如你所看到的,我必须多次计算相同的东西。这个查询只有3个派生表,但也可能是100多个,然后我必须等待10个多小时才能计算出来
UPDATE memberships SET points = points + COALESCE((
SELECT pts
FROM (
SELECT pid0, SUM (S) as Pts
FROM (
SELECT pid0, SUM (pts1) as S
FROM
(SELECT DISTINCT pid0, pid1,pts1
FROM (
SELECT t0.pid as pid0, t1.pid as pid1, t1.invitedby as i1, t1.points as pts1
FROM memberships t0, memberships t1
WHERE t0.cid = 57010 AND t1.cid = 57010) as paths
WHERE pid0 = i1) as pointsForPath
GROUP BY pid0
UNION ALL
SELECT pid0, SUM (pts2) as S
FROM (
SELECT DISTINCT pid0, pid2,pts2
FROM (
SELECT t0.pid as pid0, t1.pid as pid1, t1.invitedby as i1, t1.points as pts1 , t2.pid as pid2, t2.invitedby as i2, t2.points as pts2
FROM memberships t0, memberships t1, memberships t2
WHERE t0.cid = 57010 AND t1.cid = 57010 AND t2.cid = 57010) as paths
WHERE pid0 = i1 AND pid1 = i2) as pointsForPath
GROUP BY pid0
UNION ALL
SELECT pid0, SUM (pts3) as S
FROM (
SELECT DISTINCT pid0, pid3,pts3
FROM (
SELECT t0.pid as pid0, t1.pid as pid1, t1.invitedby as i1, t1.points as pts1 , t2.pid as pid2, t2.invitedby as i2, t2.points as pts2 , t3.pid as pid3, t3.invitedby as i3, t3.points as pts3
FROM memberships t0, memberships t1, memberships t2, memberships t3
WHERE t0.cid = 57010 AND t1.cid = 57010 AND t2.cid = 57010 AND t3.cid = 57010) as paths
WHERE pid0 = i1 AND pid1 = i2 AND pid2 = i3) as pointsForPath
GROUP BY pid0 ) as allPtsTbl
GROUP BY pid0) as temp WHERE memberships.cid = 57010 AND memberships.pid = temp.pid0 ),0)
很抱歉我的英语不好,非常感谢。如果性能是您的问题,您可以尝试:
update memberships
set points = points + coalesce(s.points, 0)
from (nasty union all'ed subquery) s
where memberships.id = s.id
(假设将pid0字段重命名为id。)
你应该考虑以下几点:
如果我错了,我很抱歉,但是你的问题看起来一团糟。我会先用using语句重构它。@PreetSangha-你是说
WITH
语句吗?如果你能解释查询试图做什么,并提供输入和预期输出的示例,那么帮助你就容易多了。@GordonLinoff我编辑了我的问题。我希望我能清楚地解释我想做什么。非常感谢,非常感谢。禁止我使用临时表或视图。有没有办法生成只计算一次路径(最长的一个子查询)的查询?这取决于您使用的数据库。对于SQL Server,您将使用递归CTE。对于Oracle,使用“连接方式”语句。但是,您可以选择在应用程序中执行此操作,为每个级别执行多个update语句。