将SQL查询转换为Codeigniter

时间:2018-09-03 09:31:43

标签: mysql sql codeigniter

嘿,有人可以帮助我将此SQL查询转换为codeigniter

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="style.css">	


</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div  id="content-wrapper" align="center">
	<div class="row">
			<img src="images/imosec_01.gif" width="1017" height="343 alt="">
	</div>

<table>
<tbody>
<tr>
<td><img src="images/imosec_02.gif" width="197" height="119"></td>
<td><img src="images/imosec_03.gif" width="128" height="138"></td>
<td><img src="images/imosec_04.gif" width="51"></td>
<td><img src="images/imosec_05.gif" width="134" height="159"></td>
<td><img src="images/imosec_06.gif" width="47"></td>
<td><img src="images/imosec_07.gif" width="149" height="201"></td>
<td><img src="images/imosec_08.gif" width="26"></td>
<td><img src="images/imosec_09.gif" width="132" height="138"></td>
<td><img src="images/imosec_10.gif" width="153" height="138"></td>
<td><img src="images/spacer.gif" width="1" height="119" alt=""></td>

</tr>


<tr>
<td><img src="images/imosec_11.gif" width="50"></td>
<td><img src="images/imosec_12.gif" width="147" height="128"/></td>
<td><img src="images/spacer.gif" width="1" height="19" alt=""></td>
</tr>


</div>

</tbody>
</table>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

marcogmonteiro已经指出-您确实必须在此处使用联接。

尝试以下

$query = $this->db
    ->select('p.*, aopd.nama_opd AS lm_jabatan_lama_opd, bopd.nama_opd AS br_jabatan_baru_opd, aunit.unit_kerja AS lm_jabatan_lama_unit, bunit.unit_kerja AS br_jabatan_baru_unit')
    ->from('sk_mutasi p')
    ->join('c1db_pegawai.m_opd aopd', 'p.jabatan_lama_opd = aopd.id', 'left')
    ->join('c1db_pegawai.m_opd bopd', 'p.jabatan_baru_opd = bopd.id', 'left')
    ->join('c1db_pegawai.m_unit_kerja aunit', 'p.jabatan_lama_unit = aunit.id', 'left')
    ->join('c1db_pegawai.m_unit_kerja bunit', 'p.jabatan_baru_unit = bunit.id', 'left')
    ->get();

答案 1 :(得分:0)

使用查询生成器,您只需将这些选择分组即可。

$this->db->select('*')
    ->group_start()
        ->select('(SELECT nama_opd FROM c1db_pegawai.m_opd aopd WHERE p.jabatan_lama_opd = aopd.id) AS lm_jabatan_lama_opd')
    ->group_end()
    ->group_start()
        ->select('(SELECT nama_opd from c1db_pegawai.m_opd bopd WHERE p.jabatan_baru_opd = bopd.id) AS br_jabatan_baru_opd')
    ->group_end()
    ->group_start()
        ->select('(SELECT unit_kerja from c1db_pegawai.m_unit_kerja aunit where p.jabatan_lama_unit = aunit.id) AS lm_jabatan_lama_unit')
    ->group_end()
    ->group_start()
        ->select('(SELECT unit_kerja from c1db_pegawai.m_unit_kerja bunit where p.jabatan_baru_unit = bunit.id) AS br_jabatan_baru_unit')
    ->group_end()
    ->from('sk_mutasi p')
    ->get()
    ->result();

您甚至可以更进一步,只需使用查询构建器来创建这些子选择。

或者,您可以像这样在查询语句中使用整个查询:

$this->db->query('SELECT *,
       (select nama_opd from c1db_pegawai.m_opd aopd where p.jabatan_lama_opd = aopd.id) lm_jabatan_lama_opd,
       (select nama_opd from c1db_pegawai.m_opd bopd where p.jabatan_baru_opd = bopd.id) br_jabatan_baru_opd,
       (select unit_kerja from c1db_pegawai.m_unit_kerja aunit where p.jabatan_lama_unit = aunit.id) lm_jabatan_lama_unit,
       (select unit_kerja from c1db_pegawai.m_unit_kerja bunit where p.jabatan_baru_unit = bunit.id) br_jabatan_baru_unit
       from sk_mutasi p')
->result();

我想指出的是,这可能不是一个很好的查询,只是进行联接以从这些额外的表中获取信息,您可能会得到更好的结果。