2016-12-01 72 views
0

這是我TABEL,與依賴如何讓mysql數據庫的foreach

表關係:data_kel

+---------+--------------+ 
| alamat | nomor_kk  | 
+---------+--------------+ 
| tes1 | 11   | 
+---------+--------------+ 
| tes2 | 12   | 
+---------+--------------+ 
| tes3 | 12   | 
+---------++-------------+ 
| tes4 | 12   | 
+---------+--------------+ 
| tes5 | 11   | 
+---------+--------------+ 

表: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   | -    | 
+--------------+---------------+ 

OUTPUT NOW(FALSE)

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

我想輸出本(右)

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

我堅持來查詢這個..其實這個查詢這麼久..它只是簡單.. 如果我使用組..全部顯示在輸出只是一個不..

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 

查詢

CREATE TABLE data_kel 
    (`alamat` varchar(5), `nomor_kk` int) 
; 

INSERT INTO data_kel 
    (`alamat` , `nomor_kk`) 
VALUES 
    ('tes1', 11), 
    ('tes2', 12), 
    ('tes3', 12), 
    ('tes4', 12), 
    ('tes5', 11) 
; 

CREATE TABLE data_survey 
    (`id_survey` int, `nomor_kk` int) 
; 

INSERT INTO data_survey 
    (`id_survey`, `nomor_kk`) 
VALUES 
    (21, 11), 
    (22, 12), 
    (23, 12), 
    (24, 12), 
    (25, 11) 
; 




CREATE TABLE rekapitulasi_kelayakan 
    (`id_srv` int, `kelayakan` varchar(5)) 
; 

INSERT INTO rekapitulasi_kelayakan 
    (`id_srv`, `kelayakan`) 
VALUES 
    (21, 'layak'), 
    (22, 'tidak'), 
    (23, '-'), 
    (24, '-'), 
    (25, '-') 
; 


CREATE TABLE data_mustahik 
    (`nama` int, `nomor_kk` int, `kepala_kel` int) 
; 

INSERT INTO data_mustahik 
    (`nama`, `nomor_kk`, `kepala_kel`) 
VALUES 
    (11, 11, 1), 
    (12, 12, 1), 
    (13, 12, 0), 
    (14, 12, 0), 
    (15, 11, 0) 
; 

查詢2

SELECT dk.nama,dl.nomor_kk,rk.kelayakan FROM data_mustahik dk 
INNER JOIN data_kel dl 
ON dk.nomor_kk=dl.nomor_kk 
INNER JOIN data_survey ds 
ON dk.nomor_kk=ds.nomor_kk 
INNER JOIN rekapitulasi_kelayakan rk 
ON ds.id_survey=rk.id_srv 
WHERE rk.kelayakan != '-' 
order by nama 
+0

這是您第三次發佈此問題。你應該擴展你的結果集如何實現。 –

+0

YESSS ..我想問問..在MySQL ..我們可以像foearch – TARA

+2

是查詢工作,並加入可能是去,但「我想分組 nomor_kk 11 NAMA 11,15 => layak nomor_kk 12路與納馬12,13,14 => tidak需要更多的解釋。「 –

回答

1

你的第二查詢正確地輸出13行。爲了讓您所需的輸出,你要麼需要刪除加入到data_kel(這似乎並不需要),或決定從該表中你想重複data_kel.nomor_kk的該公司,或使用不同的條款,像這樣

SELECT distinct dk.nama,dl.nomor_kk,rk.kelayakan FROM data_mustahik dk 
INNER JOIN data_kel dl ON dk.nomor_kk=dl.nomor_kk 
INNER JOIN data_survey ds ON dk.nomor_kk=ds.nomor_kk 
INNER JOIN rekapitulasi_kelayakan rk ON ds.id_survey=rk.id_srv 
WHERE rk.kelayakan != '-' 
order by nama 
; 
+0

謝謝youuu亞..只是截然不同 – TARA