user4393948
user4393948

Reputation:

Sum numbers in on table with MySQL

I'm using phpMyAdmin and I've this SQL table:

___SalesTaxes

|--------|----------|------------|
| STX_Id | STX_Name | STX_Amount |
|--------|----------|------------|
|      1 |    Tax 1 |       5.00 |
|      2 |    Tax 2 |      13.50 |
|--------|----------|------------|

How can I sum the total of the taxes if I know the STX_Id to sum.

For example, I need to sum the 1 and 2 to give 18.50.

What I tried:

SELECT SUM(STX_Amount) 
FROM ___SalesTaxes
WHERE FIND_IN_SET(STX_Id, '1,2') > 0
AS sum_taxes;

Thanks.

Upvotes: 1

Views: 40

Answers (2)

phpforcoders
phpforcoders

Reputation: 326

Remove 'AS sum_taxes' from the end of your query.

SELECT SUM(STX_Amount) 
FROM ___SalesTaxes
WHERE FIND_IN_SET(STX_Id, '1,2') > 0

Upvotes: 0

John Woo
John Woo

Reputation: 263713

should be using IN instead of aggregate function FIND_IN_SET.

SELECT SUM(STX_Amount) 
FROM ___SalesTaxes
WHERE STX_Id IN ('1','2') 

Here's a Demo

Upvotes: 1

Related Questions