MySQL SUM – PHP

  php

Q(Question):

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

A(Answer):

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:

A(Answer):

> >SELECT SUM(`length`)

FROM table

WHERE 1

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

Thanks for that, much appreciated.

A(Answer):

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.

A(Answer):

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:

A(Answer):

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

A(Answer):

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:

LEAVE A COMMENT