我有一些表像這樣,MySQL的多個左連接,並與條件笨多個計數
1.koperasi:
+--------+-----------------------+
| kop_id | nama |
+--------+-----------------------+
| 0 | Koperasi Sampuabalo |
| 6 | Koperasi Pinjam Lunas |
| 10 | koperasi asal pinjam |
+--------+-----------------------+
2. koperasi_anggota:
+------------+--------+------------------+
| anggota_id | kop_id | nik |
+------------+--------+------------------+
| 8 | 10 | 7868678678787832 |
| 10 | 6 | 7868678678787832 |
| 11 | 0 | 7470000000024320 |
| 12 | 10 | 7470000000024320 |
| 13 | 10 | 7470000000000002 |
+------------+--------+------------------+
3.Anggota:
+------------------+-------------------+-------------+
| nik | nama | kelamin |
+------------------+-------------------+-------------+
| 7470000000000002 | Laba Rugi | Laki - Laki |
| 7470000000024320 | Evy Rama Suciyani | Perempuan |
| 7470480283402384 | La Tundru | Laki - Laki |
| 7868678678787832 | Adelyn | Perempuan |
+------------------+-------------------+-------------+
我想是這樣創造的結果:
+----------------------+-------------------+-------------+-----------+
| koperasi_nama | man | woman | Total |
+----------------------+-------------------+-------------+-----------+
| koperasi asal pinjam | 1 | 2 | 3 |
| next row | next row | next row |next row |
+----------------------+-------------------+-------------+-----------+
我的查詢:
select c.nama as koperasi_nama,
COUNT(IF(a.kelamin = 'Laki - Laki' , 1 , 0)) as man,
COUNT(IF(a.kelamin = 'Perempuan' , 1 , 0)) as woman
from anggota a
LEFT join koperasi_anggota b on a.nik=b.nik
LEFT JOIN koperasi c on b.kop_id=c.kop_id
GROUP BY(c.kop_id)
但是,結果是這樣的:
+-----------------------+-----+-------+
| koperasi_nama | man | woman |
+-----------------------+-----+-------+
| NULL | 1 | 1 |
| Koperasi Sampuabalo | 1 | 1 |
| Koperasi Pinjam Lunas | 1 | 1 |
| koperasi asal pinjam | 3 | 3 |
+-----------------------+-----+-------+
有人可以幫助我嗎?以及如何在codeigniter上做到這一點?
請參閱:[爲什麼我應該爲我認爲是非常簡單的SQL查詢提供一個MCVE?](https://meta.stackoverflow.com/questions/333952/why-should-i-提供一個mcve爲什麼似乎對我來說是一個非常簡單的sql查詢) – Strawberry