web-dev-qa-db-ja.com

GROUP BYを使用してJOINから更新する方法

SELECTクエリで

SELECT b.id, MIN(IFNULL(a.views,0)) AS counted 
FROM table1 a JOIN table2 b ON a.id=b.id GROUP BY id 
HAVING counted>0

このクエリをUPDATEにするにはどうすればよいですか?

UPDATE b.number = counted
1
Googlebot
UPDATE table2 AS b1, ( SELECT b.id, MIN(IFNULL(a.views, 0)) AS counted 
                       FROM table1 a 
                       JOIN table2 b ON a.id = b.id 
                       GROUP BY id 
                       HAVING counted > 0 ) AS b2
SET b1.number = b2.counted
WHERE b1.id = b2.id
4
Akina
    UPDATE table2
    INNER JOIN (SELECT MIN(IFNULL(table1.views,0)) counted
    FROM table1
    GROUP BY table1.id
    HAVING counted>0
    ) x ON x.id = table2.id
    SET table2.number = x.counted
0