将数据分组但来自同一来源 - MYSQL

时间:2016-11-24 06:34:50

标签: mysql

这是我的表格,与依赖

的关系
  

表: data_mustahik

+---------+---------------+------------+
| nama    | nomor_kk      | kepala_kel |
+---------+---------------+------------+
| 11      | 11            | 1          |
+---------+--------------+-------------+
| 12      | 12            | 1          |
+---------+--------------+-------------+
| 13      | 12            | 0          |
+---------++---------------+-----------+
| 14      | 12            | 0          |
+---------+--------------+-------------+
| 15      | 11            | 0          |
+---------+--------------+-------------+
  

表: data_survey

   +--------------+---------------+
   | id_survey    | nomor_kk      |
   +--------------+---------------+
   | 21           | 11            |
   +--------------+---------------+
   | 22           | 12            |
   +--------------+---------------+
   | 23           | 12            |
   +--------------+---------------+
   | 24           | 12            |
   +--------------+---------------+
   | 25           | 11            |
   +--------------+---------------+
  

表: rekapitulasi_kelayakan

+--------------+---------------+
| id_srv       | kelayakan     |
+--------------+---------------+
| 21           | layak         |
+--------------+---------------+
| 22           | tidak         |
+--------------+---------------+
| 23           | -             |
+--------------+---------------+
| 24           | -             |
+--------------+---------------+
| 25           | -             |
+--------------+---------------+

现在输出(错误)

11 | 11 | Layak 
12 | 12 | Tidak
13 | 12 | - 
14 | 12 | -
15 | 11 | -

我想输出(右)

11 | 11 | Layak 
12 | 12 | Tidak
13 | 12 | Tidak 
14 | 12 | Tidak
15 | 11 | Layak

我坚持要查询这个..其实这个查询这么久..它只是简单.. 如果我使用组..输出只有一个不出现..

i want to grouping 
nomor_kk 11 with nama 11,15 => layak
nomor_kk 12 with nama 12,13,14 => tidak 

我的查询是

SELECT * FROM data_mustahik dk
         INNER JOIN data_survey ds 
         ON dk.nomor_kk=ds.nomor_kk
         INNER JOIN rekapitulasi_kelayakan rk
         ON ds.id_survey=rkid_srv

1 个答案:

答案 0 :(得分:0)

尝试在case语句中使用select,如下所示

case 
 when nama in (11,15) and ds.nomor_kk = 11 
  then 'Layak' 
 when nama in (12,13,14) and ds.nomor_kk = 12 
  then 'Tidak' 
end as kelayakan

希望这可以帮助你。