子查询不止一行

时间:2018-02-25 17:10:44

标签: mysql count

我的查询出了问题。我将提供我的表格数据,我有4个表格:导师,absen_user,absen,aktivitas

表导师

| id_tutor | nama   | 
| 1        | budi   |
| 2        | Gerald |

表absen_user #this表作为各种类型用户表的桥梁

| id_absen_user | id_anak_or_tutor | type  |
| 1             | 1                | tutor |
| 2             | 2                | tutor |
| 3             | 3                | child |

表absen

| id_absen    | id_aktivitas | id_absen_user | absen |
| 1           | 1            | 1             | Hadir |
| 2           | 2            | 1             | Hadir | 
| 3           | 3            | 1             | No    |
| 4           | 4            | 1             | Hadir |
| 5           | 1            | 2             | no    | 
| 6           | 2            | 2             | No    |
| 7           | 3            | 2             | Hadir |
| 8           | 4            | 2             | no    |

表aktivitas

| id_aktivitas | tanggal    |     
| 1            | 2018-02-19 |
| 2            | 2018-02-20 |
| 3            | 2018-02-21 |
| 4            | 2018-03-01 |

然后我想要的结果

| nama    | Februari |
| budi    | 67       |
| gerald  | 33       |

februari中的数据来自'计数(absen)与编码字段'Hadir'/ count(absen)* 100'......这是我的查询

SELECT nama,
       ifnull((
            SELECT ROUND(SUM(CASE absen
                            WHEN 'Hadir'
                                THEN 1
                            ELSE 0
                            END) / COUNT(absen) * 100)
            FROM (tutor)
            JOIN absen_user ON absen_user.id_anak_or_tutor = tutor.id_tutor
            JOIN absen ON absen.id_absen_user = absen_user.id_absen_user
            JOIN aktivitas ON aktivitas.id_aktivitas = absen.id_aktivitas
            WHERE (
                    (Month(aktivitas.tanggal) = 2)
                    AND (YEAR(aktivitas.tanggal) = 2018)
                    )
            GROUP BY tutor.nama
            ), 0) AS `Februari`
FROM tutor

,错误我得到subquery return more than 1 row

0 个答案:

没有答案