Jump to content

MySQL how to multiply single columns values with a specific id in a function..


Scooby08

Recommended Posts

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!

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.