自定义方式在codeigniter中编写连接,内连接mysql查询

时间:2017-10-24 06:40:29

标签: php mysql codeigniter inner-join

我有一个查询来从多个表中选择数据。如何在codeigniter中编写其等效代码。  请参阅查询:

select * 
from A inner join B on (A.ad_no=B.ad_no) 
where  B.ad_no in (select ad_no 
                   from A 
                   where $staff!='00:00' and $staff!='0:00')  
order by B.ctype asc, B.cname asc,B.ad_no asc

我在codeigniter中尝试了一个查询,但是加载结果需要更长的时间。

2 个答案:

答案 0 :(得分:1)

您可以尝试以下操作(我从工作人员处删除了$

$query = $this->db
    ->select("*")
    ->from("A")
    ->join("B", "A.ad = B.ad_no")
    ->where("B.ad_no in (select ad_no from A where staff!='00:00' and staff!='0:00')",NULL, false)
    ->order_by("B.ctype", "ASC")
    ->order_by("B.cname", "ASC")
    ->order_by("B.ad_no", "ASC")
    ->get();

使用以下语句

获得生成的输出
echo $this->db
    ->select("*")
    ->from("A")
    ->join("B", "A.ad = B.ad_no")
    ->where("B.ad_no in (select ad_no from A where staff!='00:00' and staff!='0:00')",NULL, false)
    ->order_by("B.ctype", "ASC")
    ->order_by("B.cname", "ASC")
    ->order_by("B.ad_no", "ASC")
    ->get_compiled_select();

答案 1 :(得分:0)

https://github.com/NTICompass/CodeIgniter-Subqueries/edit/master/libraries/Subquery.php

获取子查询库

尝试以下代码

$this->db->select('*')->from('A');
    $this->db->join('b','A.ad_no=B.ad_no','inner');
    $sub = $this->subquery->start_subquery('where_in');
    $sub->select('ad_no')->from('A')->where("staff!='00:00'")->where("staff!='0:00'");
    $this->subquery->end_subquery('B.ad_no', TRUE);
    $this->db->order_by('B.ctype','asc');
    $this->db->order_by('B.cname','asc');
    $this->db->order_by('B.ad_no','asc');
    $query=$this->db->get();