Jump to content

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


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

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.