Hi,
I have this MySQL table and I want to update multiple columns ( children, size) using COUNT() and SUM() on same table.
mytable
Notes:
size = privatesize + children privatesize
NULL = "", just for demonstration
Let's update for now just one column, children column. Right now I use MySQL stored functions to calculate children:
Test function getchildren:
This returns number 5
To update rows ( Example id 2 and 5 ) I use this query:
And works correctly.
But I don't want to use functions because later I need to update multiple columns (example: size ) and I don't want to call functions for every columns.
For that I have tried this query:
In fact without success, this change the value of children to NULL.
Even the same method (using the same subquery logic) doesn't work in SELECT, returns NULL or returns multiple rows ( with correct count for each group) if I change GROUP BY: id or to path. It looks like COUNT() doesn't work like usual on subquery.
What I'm missing in this query? Can someone explain what really cause this behavior or I'm wrong at all?
Online SELECT Test
Thanks.
I have this MySQL table and I want to update multiple columns ( children, size) using COUNT() and SUM() on same table.
mytable
Code:
id parentid name userid path children privatesize size ======================================================================= 1 0 Test-1 1 NULL 5 20 125 2 0 Test-2 1 NULL 0 15 15 3 1 Test-3 1 /1/ 3 25 75 4 1 Test-4 1 /1/ 0 30 30 5 3 Test-5 1 /1/3/ 0 10 10 6 3 Test-6 1 /1/3/ 1 30 40 7 6 Test-7 1 /1/3/6/ 0 10 10 8 0 Test-8 2 NULL 0 20 20
size = privatesize + children privatesize
NULL = "", just for demonstration
Let's update for now just one column, children column. Right now I use MySQL stored functions to calculate children:
Code:
DELIMITER $$ CREATE DEFINER=`dbname`@`localhost` FUNCTION `getchildren`( rowid INT, uid INT ) RETURNS INT(11) BEGIN DECLARE children INT DEFAULT 0; SELECT COUNT( `mytable`.`id` ) INTO children FROM `dbname`.`mytable` WHERE `mytable`.`path` LIKE CONCAT( '%/',rowid ,'/%' ) AND `mytable`.`userid` = uid; RETURN children; END
Code:
SELECT dbname.getchildren( 1, 1 );
To update rows ( Example id 2 and 5 ) I use this query:
Code:
UPDATE `dbname`.`mytable` SET `children` = getchildren( `mytable`.`id` , `mytable`.`userid` ) WHERE `mytable`.`id` IN ( 2, 5 );
But I don't want to use functions because later I need to update multiple columns (example: size ) and I don't want to call functions for every columns.
For that I have tried this query:
Code:
UPDATE `dbname`.`mytable` mt INNER JOIN ( SELECT `mytable`.`path` AS path, COUNT( `mytable`.`id`) AS countid FROM `dbname`.`mytable` GROUP BY `mytable`.`userid` ) sub ON `sub`.`path` LIKE CONCAT( "%/", `mt`.`id` , "/%" ) SET `mt`.`children` = `sub`.`countid` WHERE `mt`.`id` IN ( 2, 5 );
Even the same method (using the same subquery logic) doesn't work in SELECT, returns NULL or returns multiple rows ( with correct count for each group) if I change GROUP BY: id or to path. It looks like COUNT() doesn't work like usual on subquery.
What I'm missing in this query? Can someone explain what really cause this behavior or I'm wrong at all?
Online SELECT Test
Thanks.
Comment