Home > OS >  What is exactly meant by "set of values" in MySQL?
What is exactly meant by "set of values" in MySQL?

Time:08-08

A friend of mine asked me to sum up a few numbers.

Instead of using a calculator, I decided to write a SQL statement. The statement I wrote was:

select sum(100,300,200);

To my surprise, I got an error. I jumped to MySQL docs, where it says:

Aggregate functions operate on sets of values.

If [100,300,200] isn't a set of values, then what is?

Why I can't use aggregate functions in these scenarios, too?

CodePudding user response:

It means sets of values from multiple rows.

mysql> select * from mytable;
 ------ 
| num  |
 ------ 
|  100 |
|  200 |
|  300 |
 ------ 
3 rows in set (0.00 sec)

mysql> select sum(num) from mytable;
 ---------- 
| sum(num) |
 ---------- 
|      600 |
 ---------- 

If you want to sum up three scalars in one expression, just use .

mysql> select 100 300 200 as sum;
 ----- 
| sum |
 ----- 
| 600 |
 ----- 
  • Related