错误(1093):您无法在FROM子句中更新目标表以进行更新

时间:2012-11-06 06:19:29

标签: mysql

这是我得到正确答案的选择陈述:

select s_billing_cycle.Billing_cycle from s_billing_cycle 
join o_daily_lcsgeneration_copy on o_daily_lcsgeneration_copy.Location=s_billing_cycle.Location where o_daily_lcsgeneration_copy.Date between s_billing_cycle.From_Date and s_billing_cycle.To_Date

将同一个查询更新到另一个表时我无法使用更新查询获取: 您无法在FROM子句中更新目标表“o_daily_lcsgeneration_copy”以进行更新 我使用的查询是:

update o_daily_lcsgeneration_copy  set o_daily_lcsgeneration_copy.Billing_cycle = (select s_billing_cycle.Billing_cycle from s_billing_cycle join o_daily_lcsgeneration_copy on o_daily_lcsgeneration_copy.Location=s_billing_cycle.Location where o_daily_lcsgeneration_copy.Date between s_billing_cycle.From_Date and s_billing_cycle.To_Date) 

帮助我!!!

1 个答案:

答案 0 :(得分:1)

将其包装在子查询中(因此创建临时表

UPDATE  o_daily_lcsgeneration_copy
SET     o_daily_lcsgeneration_copy.Billing_cycle = 
(
    SELECT Billing_cycle
    FROM
    (
        SELECT  s_billing_cycle.Billing_cycle
        FROM    s_billing_cycle
                INNER JOIN o_daily_lcsgeneration_copy
                    ON o_daily_lcsgeneration_copy.Location = s_billing_cycle.Location
        WHERE   o_daily_lcsgeneration_copy.DATE BETWEEN s_billing_cycle.From_Date
                AND s_billing_cycle.To_Date
    ) s
)

JOIN表格

UPDATE  o_daily_lcsgeneration_copy a
        INNER JOIN s_billing_cycle b
            ON a.Location = b.Location
SET     a.Billing_cycle = b.Billing_cycle
WHERE   a.DATE BETWEEN b.From_Date AND b.To_Date