I have a query to select data from multiple tables. How do I write its equivalent code in codeigniter. See the query:
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
I tried a query in codeigniter but its taking longer time to load the result.
You can try the following (i removed the $
sign from staff)
$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();
You get a generated output with the following statement
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();
get subquery library from https://github.com/NTICompass/CodeIgniter-Subqueries/edit/master/libraries/Subquery.php
try the following code
$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();