0

I have 4 column in which i wanna get two sum values for 2-2 column individually

I am trying this

   SELECT (SUM (`user1`) + SUM(`user2`)),(SUM (`customer1`) + SUM(`customer2`)) 
   AS (`totalUsers`,`totalCustomers`) FROM `collection`

it was working for

  SELECT SUM (`user1`) + SUM(`user2`) AS `totalUsers` FROM `collection`

I was giving result

 RowDataPacket { totalUsers: 3345 }

So I thought of doing that in similar fashion,but it is not working for multiple sum result.It is giving

Unhandled rejection Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS totalUsers FROM collection' at line 1

How do i achieve this?

1 Answer 1

1

You need to define aliases individually for an expression/column.

SELECT (SUM (`user1`) + SUM(`user2`)) AS `totalUsers`,
       (SUM (`customer1`) + SUM(`customer2`)) AS `totalCustomers`
FROM `collection`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.