2011/11/14

[MySQL] UPDATE from SELECT result

需求:統計各分類的文章數量,並將數量寫入分類資料表。
語法如下:
UPDATE t1 AS A,
 (SELECT `id`, COUNT(`id`) AS `no` FROM t2 GROUP BY `id`) AS B
 SET A.`no`=B.`no` WHERE A.`id`=B.`id`;