I have a news table where each record have fields:
news_id (INT), parent_id (INT) , subitems (INT)
I want each record to carry the total (count) of direct children of it
e.g. (I want the result to be like that)
news_id: 1, parent_id: 0, subitems: 2
news_id: 2, parent_id: 1 , subitems: 0
news_id: 3, parent_id: 1 , subitems: 0
How do I do something like this correctly:
UPDATE news n
SET subitems = (SELECT COUNT(*) FROM `news` AS `n2`
WHERE `n2`.`parent_id` = `n`.`news_id`)
as the above gives the following error:
#1093 - You can't specify target table 'n' for update in FROM clause