View Full Version : sum(number)

BubikolRamios

03-02-2013, 12:16 PM

select sum(111) = 111

now try:

select sum(111) from xtable

select sum(111) from ytable

any you get some looks like randome numbers.

Any explanation ?

Arcticwarrio

03-05-2013, 09:54 PM

what is xtable ?

BubikolRamios

03-05-2013, 10:41 PM

it is any table.

Arcticwarrio

03-05-2013, 11:07 PM

its the number of entrys 3 times eg if you have 51 rows it will output 515151

Arcticwarrio

03-05-2013, 11:10 PM

or not, 4 rows = 444

31 rows = 3441

so its 111 * rows

Old Pedant

03-05-2013, 11:32 PM

Correct. 55 rows would be 5661.

It's easy to see this, Bubikol, if you do

SELECT COUNT(*), SUM(111) FROM xtable

What you are telling SQL (any SQL, not just MySQL) to do is to add 111 to the sum for each row in the table.

Think about it: If you do

SELECT SUM(someField) FROM xtable

all that tells SQL to do is to add the value of field someField to the sum for each row in the table. So when you use a constant, instead, it is the constant that gets added for each row.

Nothing "random" about it.

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.