1

Does MySQL have any functions like SUM but then do the MULTIPLICATION of a collumn?

Example: I have an table like this, and I want to multiply the entire collumn

+--------------+
| combinations |
+--------------+
|            3 |
|            5 |
|            4 |
|            6 |
|            1 |
|            3 |
|            4 |
+--------------+

expected result:

+----------------------------+
| product_total_combinations |
+----------------------------+
|                       4320 |
+----------------------------+

I cannnot find anywhere how to achieve this. Any help is appreciated.

0

1 Answer 1

5

As explained by How do I create a user-defined aggregate function? there is no such aggregate function (also see here).

However you can emulate your product by:

log(product(x) ) = sum(log(x))

SELECT EXP(SUM(LOG(a))) AS product
FROM t ;

Beware, there could be rounding errors. If this is all integers, do ROUND(...) around the EXP.

2
  • Yup this worked, thank you. I searched all morning but couldn't find any questions similar to mine.
    – Ludisposed
    Commented Jul 21, 2017 at 7:48
  • Beware, there could be rounding errors. If this is all integers, do ROUND(...) around the EXP.
    – Rick James
    Commented Jul 21, 2017 at 16:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.