Scooby08 Posted March 5, 2011 Share Posted March 5, 2011 Hello.. I'm just wondering if there is a way to multiply a single columns values using a stored function.. Example table: (my_table) id my_id multiply 0 1 2 1 1 2 2 3 2 3 3 2 4 3 2 5 3 2 Now I will never know what the multiply number will be.. I'm using 2 as an example.. Here's pretty close to what I was thinking, but I just don't know how to multiply the fields.. Do I need to run a loop or something like that? CREATE FUNCTION multiply(in_my_id INT) RETURNS DECIMAL(10,2) BEGIN DECLARE total DECIMAL(10,2); SET total = (SELECT multiply FROM my_table WHERE my_id = in_my_id); -- loop here?? RETURN total; END; If in_my_id = 1, then it should return 4.. If in_my_id = 3, then it should return 16.. Thanks for any replies! Link to comment https://forums.phpfreaks.com/topic/229707-mysql-how-to-multiply-single-columns-values-with-a-specific-id-in-a-function/ Share on other sites More sharing options...
Scooby08 Posted March 5, 2011 Author Share Posted March 5, 2011 Found my answer finally.. Thanks.. CREATE FUNCTION multiply(in_my_id INT) RETURNS DECIMAL(10,2) BEGIN DECLARE total DECIMAL(10,2); SET total = (SELECT EXP(SUM(LOG(COALESCE(multiply,1)))) FROM my_table WHERE my_id = in_my_id); RETURN total; END; http://lists.mysql.com/mysql/166184 Link to comment https://forums.phpfreaks.com/topic/229707-mysql-how-to-multiply-single-columns-values-with-a-specific-id-in-a-function/#findComment-1183327 Share on other sites More sharing options...
Scooby08 Posted March 5, 2011 Author Share Posted March 5, 2011 I also found out that LOG is not defined for negative values.. http://forums.mysql.com/read.php?117,219696,264407#msg-264407 Link to comment https://forums.phpfreaks.com/topic/229707-mysql-how-to-multiply-single-columns-values-with-a-specific-id-in-a-function/#findComment-1183331 Share on other sites More sharing options...
fenway Posted March 6, 2011 Share Posted March 6, 2011 That's a math issue, not a mysql issue. Link to comment https://forums.phpfreaks.com/topic/229707-mysql-how-to-multiply-single-columns-values-with-a-specific-id-in-a-function/#findComment-1183515 Share on other sites More sharing options...
Scooby08 Posted March 6, 2011 Author Share Posted March 6, 2011 Well then move it.. Link to comment https://forums.phpfreaks.com/topic/229707-mysql-how-to-multiply-single-columns-values-with-a-specific-id-in-a-function/#findComment-1183524 Share on other sites More sharing options...
fenway Posted March 6, 2011 Share Posted March 6, 2011 Well then move it.. There's no "log of a negative number is imaginary" forum. Link to comment https://forums.phpfreaks.com/topic/229707-mysql-how-to-multiply-single-columns-values-with-a-specific-id-in-a-function/#findComment-1183551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.