mysqlのSUM() 関数に疑問があります。条件なしで SUM() 関数を使用している場合は正常に動作します。しかし、条件付きでSUM()関数を使用すると、条件に一致するレコードの数が返されます。これを説明してください。
以下のように、4 つの列を持つ 1 つのテーブル Teacher を作成しました。
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)