0

我是初学者,需要一些帮助。尝试使用来自多个表的数据更新一个表,并在几个月或几周内对它们进行分组

update monthly_report
SET De_Ative = (SELECT IFNULL(sum(statuschange.NUM_STATUS_CHANGES),0) FROM statuschange
WHERE month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')) = monthly_report.Mon
AND statuschange.STATUS_CHANGE ='CANCEL' OR statuschange.STATUS_CHANGE ='LOCK'
group by month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')))

[SQL] update monthly_report
SET De_Ative = (SELECT IFNULL(sum(statuschange.NUM_STATUS_CHANGES),0) FROM statuschange
WHERE month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')) = monthly_report.Mon
AND statuschange.STATUS_CHANGE ='CANCEL' OR statuschange.STATUS_CHANGE ='LOCK'
group by month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')))

-- group by monthly_report.Mon

[Err] 1242 - Subquery returns more than 1 row
4

1 回答 1

0

试试这个,

UPDATE  monthly_report a
        LEFT JOIN
        (
            SELECT  MONTH(STR_TO_DATE(a.Evaluation_day,'%d.%m.%Y')) mnt,
                    SUM(a.NUM_STATUS_CHANGES) totalSum
            FROM    statuschange a
            WHERE   a.STATUS_CHANGE IN ('CANCEL', 'LOCK')
            GROUP   BY MONTH(STR_TO_DATE(a.Evaluation_day,'%d.%m.%Y'))
        ) b ON b.mnt = a.mon
SET     a.De_Ative = IFNULL(b.totalSum, 0)
于 2013-09-23T10:14:47.133 回答