Как конвертировать запрос из sql в codeigniter - PullRequest
0 голосов
/ 28 февраля 2019

Это запрос, который мне нужно преобразовать из SQL в CodeIgniter:

SELECT b.bulan_tahun,b.kec, SUM(b.LFormal) AS LFormal, SUM(b.PFormal) AS PFormal, SUM(b.LFormal)+SUM(b.PFormal) AS SUMFormal,
SUM(b.LInformal) AS LInformal, SUM(b.PInformal) AS PInformal, SUM(b.LInformal)+SUM(b.PInformal) AS SUMInformal, SUM(b.LFormal)+ SUM(b.LInformal) AS LJumlah, 
SUM(b.PFormal)+SUM(b.PInformal) AS PJumlah,SUM(b.LFormal)+SUM(b.LInformal)+SUM(b.PFormal)+SUM(b.PInformal) AS SUMJUMLAH FROM
(SELECT a.bulan_tahun,a.kec,
(CASE WHEN a.sktor_pkrjaan='FORMAL' AND a.jk='L' THEN JJK ELSE 0 END) AS LFormal,
(CASE WHEN a.sktor_pkrjaan='FORMAL' AND a.jk='P' THEN JJK ELSE 0 END) AS PFormal,
(CASE WHEN a.sktor_pkrjaan='INFORMAL' AND a.jk='L' THEN JJK ELSE 0 END) AS LInformal,
(CASE WHEN a.sktor_pkrjaan='INFORMAL' AND a.jk='P' THEN JJK ELSE 0 END) AS PInformal
FROM
(SELECT id_tki,bulan_tahun,kec, COUNT(jk) AS JJK, sktor_pkrjaan, jk 
FROM tki
GROUP BY id_tki,bulan_tahun,kec, sktor_pkrjaan, jk) a
WHERE a.JJK>0) b
GROUP BY b.bulan_tahun,b.kec

Я попытался выполнить поиск в Active Record CodeIgniter, но не смог получить ответ.

Спасибо.

Ответы [ 2 ]

0 голосов
/ 28 февраля 2019

Вы можете попробовать вот так, разбивая ваш необработанный запрос по активной записи.

this->db->select("
SELECT b.bulan_tahun,b.kec, SUM(b.LFormal) AS LFormal, SUM(b.PFormal) AS PFormal, SUM(b.LFormal)+SUM(b.PFormal) AS SUMFormal,
SUM(b.LInformal) AS LInformal, SUM(b.PInformal) AS PInformal, SUM(b.LInformal)+SUM(b.PInformal) AS SUMInformal, SUM(b.LFormal)+ SUM(b.LInformal) AS LJumlah, 
SUM(b.PFormal)+SUM(b.PInformal) AS PJumlah,SUM(b.LFormal)+SUM(b.LInformal)+SUM(b.PFormal)+SUM(b.PInformal) AS SUMJUMLAH 
");
$this->db->from("
    (SELECT a.bulan_tahun,a.kec,
(CASE WHEN a.sktor_pkrjaan='FORMAL' AND a.jk='L' THEN JJK ELSE 0 END) AS LFormal,
(CASE WHEN a.sktor_pkrjaan='FORMAL' AND a.jk='P' THEN JJK ELSE 0 END) AS PFormal,
(CASE WHEN a.sktor_pkrjaan='INFORMAL' AND a.jk='L' THEN JJK ELSE 0 END) AS LInformal,
(CASE WHEN a.sktor_pkrjaan='INFORMAL' AND a.jk='P' THEN JJK ELSE 0 END) AS PInformal
(SELECT id_tki,bulan_tahun,kec, COUNT(jk) AS JJK, sktor_pkrjaan, jk 
FROM tki
GROUP BY id_tki,bulan_tahun,kec, sktor_pkrjaan, jk) a
WHERE a.JJK>0) b
");
$this->db->group_by("b.bulan_tahun,b.kec");
$data = $this->db->get()->result_array();
0 голосов
/ 28 февраля 2019

Как это

$sql = "your query here";
$this->db->query($sql)->result();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...