I am using a MySQL function to find out the max record in that table.
SQL data:
id_ index Value1 Value2 Value 3 Max_idVal 1 'abc' 5 10 5 5 1 'abc' 0 12 4 5 1 'abc' 0 13 3 5 2 'abc' 4 9 10 8 2 'abc' 8 10 8 8
Max_idVal
is the result returned by the MySQL function.
The idea is to go through each row and put the maximum value into the Max_idVal
field for that row.
Here for ID_
1 the Value1
is the largest value at '5'. That gets assigned to Max_idVal'. This works the same way for
ID_` 2 and the rest of the rows.
SQL function:
DELIMITER $$
CREATE FUNCTION `fn_get_max` (
_id INT,
_index VARCHAR( 30 )
) RETURNS INT( 11 ) READS SQL DATA BEGIN DECLARE r INT;
SELECT Max(value1 )
INTO r
FROM Table_name
WHERE id = _id
AND index = _index;
RETURN r;
END $$
SQL query:
UPDATE table_name SET max_idval = fn_get_max('1','abc') WHERE id = '1'
My problem is that this returns NULL
rows even though the ID matches a record.
What am I doing wrong?