2017-07-20 128 views
-1

我有一些表像這樣,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 | 
+-----------------------+-----+-------+ 

mysql multiple left join and multiple count with condition codeigniter

有人可以幫助我嗎?以及如何在codeigniter上做到這一點?

+0

請參閱:[爲什麼我應該爲我認爲是非常簡單的SQL查詢提供一個MCVE?](https://meta.stackoverflow.com/questions/333952/why-should-i-提供一個mcve爲什麼似乎對我來說是一個非常簡單的sql查詢) – Strawberry

回答

1

https://www.codeigniter.com/userguide3/database/query_builder.html

//heres how you convert your current query to CI query builder 

$query = $this->db->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 ') 
//equivlant to :LEFT join koperasi_anggota b on a.nik=b.nik 
->join('koperasi_anggota b', 'a.nik=b.nik', 'left') 
->join('koperasi c', 'b.kop_id=c.kop_id', 'left') 
->group_by('c.kop_id') 
//from anggota a ->return results as an array, can also do result() to return as objects 
->get('anggota a')->result_array(); 

您也可以施放原始SQL的變量,然後查詢它:

$sql = "SELECT k.nama as koperasi_nama, 
sum(a.kelamin = 'Laki - Laki') man, 
sum(a.kelamin = 'Perempuan') woman, 
count(a.kelamin) total 
FROM koperasi k INNER JOIN koperasi_anggota ka ON k.kop_id = ka.kop_id 
INNER JOIN anggota a ON ka.nik = a.nik 
group by k.kop_id"; 

$results = $this->db->query($sql)->result_array(); 
+0

whoa感謝fpr你的快速回應@Parker戴爾,這項工作完美! – wahmal

+0

好的方法來複制他人的答案並在你的編輯。 –

+0

但是,如何再次用外部表koperasi_anggota添加1個計數? – wahmal

1

嘗試以下查詢:

SELECT k.nama as koperasi_nama, 
sum(a.kelamin = 'Laki - Laki') man, 
sum(a.kelamin = 'Perempuan') woman, 
count(a.kelamin) total 
FROM koperasi k INNER JOIN koperasi_anggota ka ON k.kop_id = ka.kop_id 
INNER JOIN anggota a ON ka.nik = a.nik 
group by k.kop_id; 

輸出:

+-----------------------+------+-------+-------+ 
| koperasi_nama   | man | woman | total | 
+-----------------------+------+-------+-------+ 
| Koperasi Sampuabalo | 0 |  1 |  1 | 
| Koperasi Pinjam Lunas | 0 |  1 |  1 | 
| koperasi asal pinjam | 1 |  2 |  3 | 
+-----------------------+------+-------+-------+