mysql sum函数问题

时间:2013-01-08 12:40:13

标签: mysql aggregate-functions

我对mysql中的 SUM()函数 有些怀疑。当我使用SUM()函数没有条件将工作正常。但是如果我将条件使用SUM()函数,它将返回与条件匹配的记录数。解释一下。

我创建了一个表教师,有四列,如下所示。

mysql> desc Teacher;


+--------------+-------------+------+-----+-------------------+
| Field        | Type        | Null | Key | Default           |
+--------------+-------------+------+-----+-------------------+
| TeacherId    | varchar(36) | NO   | PRI | NULL              |
| FirstName    | varchar(50) | NO   |     | NULL              |
| LastName     | varchar(50) | NO   |     | NULL              |
| SyncStatus   | int(11)     | NO   |     | NULL              |
+--------------+-------------+------+-----+-------------------+

它包含在记录之后。

mysql> select * from Teacher;

+-----------+-----------+-----------+------------+
| TeacherId | FirstName | LastName  | SyncStatus |
+-----------+-----------+-----------+------------+
| 001       | Sagar     | Kapadia   |          1 |
| 002       | Vishal    | Chaudhari |          1 |
| 003       | Milan     | Panchal   |          1 |
| 004       | Jugal     | Desai     |          1 |
| 005       | Baburao   | Apte      |          1 |
| 006       | Jaya      | Bacchan   |          0 |
| 007       | Jaya      | Bacchan   |          2 |
| 008       | Jaya      | Bacchan   |          2 |
+-----------+-----------+-----------+------------+
8 rows in set (0.00 sec)




mysql> select TeacherId,FirstName,LastName,Concat(FirstName,' ',LastName) as FullName,SUM(SyncStatus) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+-----------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus) |
+-----------+-----------+----------+---------------+-----------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |               9 |
+-----------+-----------+----------+---------------+-----------------+
1 row in set (0.00 sec)

所以它会在同步状态下返回完美的总和。但是,如果我将条件使用SUM(),则返回记录数。

mysql> select TeacherId,FIrstName,LastName,Concat(FirstName,' ',LastName) as FullName,sum(SyncStatus = 0) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+---------------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus = 0) |
+-----------+-----------+----------+---------------+---------------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |                   1 |
+-----------+-----------+----------+---------------+---------------------+
1 row in set (0.00 sec)



mysql> select TeacherId,FIrstName,LastName,Concat(FirstName,' ',LastName) as FullName,sum(SyncStatus = 1) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+---------------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus = 1) |
+-----------+-----------+----------+---------------+---------------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |                   5 |
+-----------+-----------+----------+---------------+---------------------+
1 row in set (0.00 sec)



mysql> select TeacherId,FIrstName,LastName,Concat(FirstName,' ',LastName) as FullName,sum(SyncStatus = 2) from Teacher having FullName like '_____ %';
+-----------+-----------+----------+---------------+---------------------+
| TeacherId | FIrstName | LastName | FullName      | sum(SyncStatus = 2) |
+-----------+-----------+----------+---------------+---------------------+
| 001       | Sagar     | Kapadia  | Sagar Kapadia |                   2 |
+-----------+-----------+----------+---------------+---------------------+
1 row in set (0.00 sec)

1 个答案:

答案 0 :(得分:3)

你是对的。此函数sum(SyncStatus = 2)将返回行数,因为这是执行的方式

在每一行中SyncStatus = 2将测试SyncStatus equals 2并返回1(true) or 0(false),因此您基本上得到了真值的总和(1)