如何解决错误"子查询返回超过1行"在mysql中?

时间:2018-05-27 09:10:21

标签: mysql codeigniter

我在codeigniter

中进行了如下查询
$query = $this->db->query('SELECT tanggal,
            ( SELECT COUNT(*) from absensi_siswa where kehadiran="alfa" GROUP BY tanggal ) AS alfa,
            ( SELECT COUNT(*) from absensi_siswa where kehadiran="izin" GROUP BY tanggal ) AS izin,
            ( SELECT COUNT(*) from absensi_siswa where kehadiran="hadir" GROUP BY tanggal ) AS hadir,
            ( SELECT COUNT(*) from absensi_siswa where kehadiran="sakit" GROUP BY tanggal ) AS sakit
            FROM absensi_siswa GROUP BY tanggal');
    return $query->result();

1 个答案:

答案 0 :(得分:0)

您的每个子句[2018-05-27 11:27:42,823] Artifact bets:war: Artifact is deployed successfully [2018-05-27 11:27:42,823] Artifact bets:war: Deploy took 13 582 milliseconds 返回的行数超过1行,从您的查询中我假设您需要根据(select count(*) .. )的不同值进行个别计数,您可以将查询简化为

kehadiran

使用mysql可以使SELECT tanggal, SUM(CASE WHEN kehadiran="alfa" THEN 1 ELSE 0 END) AS alfa, SUM(CASE WHEN kehadiran="izin" THEN 1 ELSE 0 END) AS izin, SUM(CASE WHEN kehadiran="hadir" THEN 1 ELSE 0 END) AS hadir, SUM(CASE WHEN kehadiran="sakit" THEN 1 ELSE 0 END) AS sakit FROM absensi_siswa GROUP BY tanggal 子句更短,如sum,将表达式放在sum(kehadiran="alfa") as alfa中将返回布尔值0/1或true / false,您将得到一个基于计数的在你的表达