luqita
luqita

Reputation: 4077

Counting a column in MySQL

Suppose that I have a column named '$' and in it I have different values, for example:

id|| $ 
======
1 || 50
======
2 || 54
======
3 || 76
======

How can i add the values of $? A query that would give me 50+54+76? The real table has around 20,000 rows.

Thanks!

Upvotes: 3

Views: 128

Answers (3)

Patrick87
Patrick87

Reputation: 28302

select sum($) from table_name;

Upvotes: 2

Cfreak
Cfreak

Reputation: 19309

Use the SUM() function. Since I don't believe you can have a column named $ (and even if you can, it seems there would be unintended consequences), I've named your column amount

SELECT SUM(amount) AS total FROM your_table;

Edit As mentioned by @Brendan you can have a column named $. I stand by my opinion that it would be confusing in code :)

Upvotes: 0

Brendan Bullen
Brendan Bullen

Reputation: 11798

SELECT SUM($) FROM your_table_name

I'm not sure $ is a valid column name though

UPDATE Tested in PHPMyAdmin and I was able to name a column $ and perform a SUM on it. Something new learned!

Upvotes: 7

Related Questions