如何将下面的SQL查询写入代码点火器
SELECT
`pm`.`id`,
`pm`.`region`,
`pm`.`port_name`,
`pm`.`country`,
`tbl_port_annual_throughput`.`YEAR`,
`tbl_port_annual_throughput`.`TOTAL_THROUGHPUT`,
`tbl_port_annual_throughput`.`status`
FROM (`tbl_port_annual_throughput`)
JOIN `port_master` pm
ON `pm`.`id` = `tbl_port_annual_throughput`.`PORT_ID`
WHERE `pm`.`country` = "100@Kenya"
AND (
(`tbl_port_annual_throughput`.`YEAR` = '2014' AND `tbl_port_annual_throughput`.`status` = 'Confirmed')
OR (`tbl_port_annual_throughput`.`YEAR` = '2015' AND `tbl_port_annual_throughput`.`status` = 'Confirmed')
OR (`tbl_port_annual_throughput`.`YEAR` = '2016' AND `tbl_port_annual_throughput`.`status` = 'Confirmed')
)
答案 0 :(得分:2)
试试这个:
$or_where= "(`tbl_port_annual_throughput`.`YEAR` = '2014' AND `tbl_port_annual_throughput`.`status` = 'Confirmed') OR (`tbl_port_annual_throughput`.`YEAR` = '2015' AND `tbl_port_annual_throughput`.`status` = 'Confirmed') OR (`tbl_port_annual_throughput`.`YEAR` = '2016' AND `tbl_port_annual_throughput`.`status` = 'Confirmed')";
$this->db->select('pm.id, pm.region,pm.port_name,pm.country,tbl_port_annual_throughput.YEAR,tbl_port_annual_throughput.TOTAL_THROUGHPUT,tbl_port_annual_throughput.status');
$this->db->join('port_master as pm','pm.id = tbl_port_annual_throughput.PORT_ID');
$this->db->where('pm.country','100@Kenya');
$this->db->where($or_where);
return $this->db->get('tbl_port_annual_throughput')->result();
答案 1 :(得分:0)
您可以使用查询构建器类:
$this->db->select
$this->db->from
$this->db->join
这些方法可用于在查询中执行相同的操作
您可以在此处找到更多信息:Linkylink
答案 2 :(得分:0)
在你转换它之前,实际的SQL需要一点点整理......
SELECT
`pm`.`id`,
`pm`.`region`,
`pm`.`port_name`,
`pm`.`country`,
`pat`.`YEAR`,
`pat`.`TOTAL_THROUGHPUT`,
`pat`.`status`
FROM `tbl_port_annual_throughput` pat
JOIN `port_master` pm
ON `pm`.`id` = `pat`.`PORT_ID`
WHERE `pm`.`country` = "100@Kenya"
AND `pat`.`YEAR` IN ('2014','2015','2016')
AND `pat`.`status` = 'Confirmed'
所以现在它更简单,工作 - 快速模拟你的表格......
注意:请确保以上内容确实适用于您。
转发那个。
答案 3 :(得分:0)
如果你真的想要你的确切查询,你可以做这样的事情
$query = $this->db
->select('pm.id, pm.region, pm.port_name, tbl_port_annual_throughput.YEAR, tbl_port_annual_throughput.TOTAL_THROUGHPUT, tbl_port_annual_throughput.status')
->from('tbl_port_annual_throughput')
->join('port_master pm', 'pm.id = tbl_port_annual_throughput.PORT_ID')
->where('pm.country','100@Kenya')
->group_start()
->group_start()
->where('tbl_port_annual_throughput.YEAR','2014')
->where('tbl_port_annual_throughput.status','Confirmed')
->group_end()
->or_group_start()
->where('tbl_port_annual_throughput.YEAR','2015')
->where('tbl_port_annual_throughput.status','Confirmed')
->group_end()
->or_group_start()
->where('tbl_port_annual_throughput.YEAR','2016')
->where('tbl_port_annual_throughput.status','Confirmed')
->group_end()
->group_end()
->get();
答案 4 :(得分:0)
您可以执行此查询
$query = $this->db
->select('pm.id, pm.region, pm.port_name, tbl_port_annual_throughput.YEAR, tbl_port_annual_throughput.TOTAL_THROUGHPUT, tbl_port_annual_throughput.status')
->from('tbl_port_annual_throughput')
->join('port_master pm', 'pm.id = tbl_port_annual_throughput.PORT_ID')
->where('pm.country','100@Kenya')
->group_start()
->group_start()
->where('tbl_port_annual_throughput.YEAR','2014')
->where('tbl_port_annual_throughput.status','Confirmed')
->group_end()
->group_end()
->get();
答案 5 :(得分:0)
$wherearraydata1 = array('tbl_port_annual_throughput.YEAR' => '2014', 'tbl_port_annual_throughput.status' => 'Confirmed');
$wherearraydata2 = array('tbl_port_annual_throughput.YEAR' => '2015', 'tbl_port_annual_throughput.status' => 'Confirmed');
$wherearraydata3 = array('tbl_port_annual_throughput.YEAR' => '2016', 'tbl_port_annual_throughput.status' => 'Confirmed');
$this->db->select('pm.id,pm.region,pm.port_name,pm.country,tbl_port_annual_throughput.YEAR,tbl_port_annual_throughput.TOTAL_THROUGHPUT,tbl_port_annual_throughput.status');
$this->db->from('tbl_port_annual_throughput');
$this->db->join('port_master', 'port_master.id = tbl_port_annual_throughput.PORT_ID');
$this->db->where('pm.country','100@Kenya');
$this->db->where("( $wherearraydata1 OR $wherearraydata2 OR $wherearraydata3)");
$querydata = $this->db->get();
return $querydata->result_array();