2

我对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)
4

1 回答 1

3

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

在每一行SyncStatus = 2中都会测试 ifSyncStatus equals 2和 returns 1(true) or 0(false),所以你基本上得到了真值的总和(1)

于 2013-01-08T12:46:44.750 回答