rocks
rocks

Reputation: 190

query for retrieving product(column) in mysql

say for an example i have employee table with id,name,salary

id |name|salary
1  |raj |50.00
2  |raaj|60.00
3  |nera|555.55

i want to multiply the salary field 50*60*555.55=1666650.00

salary
------
1666650.00

Upvotes: 2

Views: 1666

Answers (3)

Afarin N.
Afarin N.

Reputation: 1

Try the following code in which @comb represents a constant that you initialize to 1 and for every row gets accumulated with its product to salary.

 SET @comb = 1;
    SELECT @comb:= @comb* salary
    FROM employee

Upvotes: -1

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16076

Hey you can try this-

select exp(sum(log(coalesce(salary,1)))) as d from employee

You can get more information on

http://lists.mysql.com/mysql/166184

Upvotes: 1

Dinup Kandel
Dinup Kandel

Reputation: 2505

i think you want the sum of all salary column if you want

select sum(salary)as salary from employee

if you want product then it is

  select   round(EXP(SUM(LOG(salary))),2) as salary from employee 

see the example for different rounding in mysql

  SELECT ROUND( 1 );   /* = 1 */
    SELECT ROUND( 1.4 ); /* = 1 */
    SELECT ROUND( 1.5 ); /* = 2 */

    SELECT ROUND( -1.4 ); /* = -1 */
    SELECT ROUND( -1.5 ); /* = -2 */

    SELECT ROUND( 1.4212, 1 ); /* = 1.4 */
    SELECT ROUND( 1.4512, 1 ); /* = 1.5 */

Upvotes: 2

Related Questions