469,290 Members | 1,789 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

MySQL SUM

Hey there,

I've been searching for a way to add up a number of numbers from a MySQL
database and have come up with the SUM() statement, but I'm not really too
sure how to use it. The manual doesn't give me much in the way of
assistance...

SELECT SUM(`length`) WHERE 1

doesn't work at all. Any ideas? What am I doing wrong, I'm fairly sure it is
going to be something very silly.

Thanks

Jamie
Jul 16 '05 #1
6 13366
Jamie Wright wrote:
SELECT SUM(`length`) FROM tableWHERE 1


Suppose you have this table (named "distance")

id | length
---+--------
1 | 17,82
2 | 190.02
3 | 3.99
4 | 77.76

The result of

"select sum(length) from distance"

is 289.59

and "select sum(length) from distance where id<3"

returns 207.84

HTH
--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #2
> >SELECT SUM(`length`)
FROM table
WHERE 1


like I said - it was going to be something silly!

Thanks for that, much appreciated.
Jul 16 '05 #3
Jamie Wright wrote:
SELECT SUM(`length`)


FROM table
WHERE 1

like I said - it was going to be something silly!

Thanks for that, much appreciated.


Use ' instead of ` ... I don't think MySQL likes backticks.

Jul 16 '05 #4
Joshua Ghiloni wrote:
Use ' instead of ` ... I don't think MySQL likes backticks.


Backticks in MySQL are used to delimit column names. You can have a
column named, for instance, "from" and the only way to access it is
with the backticks.

The single quotes are used to delimit text, as well as the double
quotes
select id from table where `from` = 'TX'

or

select id from table where `from` = "TX"

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #5
Pedro wrote:
Joshua Ghiloni wrote:
Use ' instead of ` ... I don't think MySQL likes backticks.


Backticks in MySQL are used to delimit column names. You can have a
column named, for instance, "from" and the only way to access it is
with the backticks.


Only way? I must have missed something...

Why use backticks at all??

Just put the name of a column, as it is... without quotes or backticks or
anything :)

--
--- --- --- --- --- --- ---
ja**@croatiabiz.com
Jul 16 '05 #6
jack wrote:
Pedro wrote:
Backticks in MySQL are used to delimit column names. You can have a
column named, for instance, "from" and the only way to access it is
with the backticks.
Only way? I must have missed something...


Did you notice the column name?
Why use backticks at all??

Just put the name of a column, as it is... without quotes or backticks or
anything :)


Try it:

create table xxx (
id int UNSIGNED NOT NULL AUTO_INCREMENT,
`from` char(2) NOT NULL,
PRIMARY KEY(id)
)

insert into xxx values (NULL, 'TX'), (NULL, 'WA'), (NULL, 'OH')

select * from xxx where from = 'TX'
-- error on the previous line;

select * from xxx where `from` = 'TX'
--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by mikey | last post: by
reply views Thread by Yun Guan | last post: by
reply views Thread by Mike Chirico | last post: by
2 posts views Thread by Saqib Ali | last post: by
2 posts views Thread by trihanhcie | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.