mysql多次左连接和多次计数与条件codeigniter

时间:2017-07-20 11:55:27

标签: php mysql codeigniter-3

我有一些像这样的表,

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上做到这一点?

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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 |
+-----------------------+------+-------+-------+