SELECT *
FROM fx_dirprocess
WHERE `pro_id` IN (SELECT MAX(`pro_id`) FROM fx_dirprocess GROUP BY name)
AND `co_id`=$company;
How to construct this query in codeigniter
Following I have tried and no use:
$this->db->select('*')->from('fx_dirprocess');
$this->db->where('`pro_id` IN (SELECT MAX(`pro_id`)
FROM `fx_dirprocess`
GROUP BY `name`,’co_id’,$company)’, NULL, FALSE);
$this->db->select_max(‘pro_id’)->group_by(‘name’);
$where_clause = $this->db->get_compiled_select('fx_dirprocess');
$this->db->select('*');
$this->db->from('fx_dirprocess');
$this->db->where("`pro_id` IN ($where_clause)", NULL, co_id,$company);
@Tejaswini please check the below code hope it will help.
// make A subquery :-
$this->db->select_max('pro_id')->from('fx_dirprocess')->group_by('name');
$subQuery = $this->db->get_compiled_select();
// Now the main query is :-
$this->db->select('*')->from('fx_dirprocess');
$this->db->where("pro_id IN ($subQuery)", NULL, FALSE);
$this->db->get()->result();
Thanks!
You are passing GROUP BY
for selecting MAX
value, it won't give exact value. you like following
$sql_query = "SELECT * FROM `fx_dirprocess` WHERE `pro_id` IN (SELECT MAX(`pro_id`) FROM `fx_dirprocess`) AND `co_id` = '{$company}'";
$query = $this->db->query($sql_query);
return ($query->num_rows() > 1) ? $query->row() : $query->result();
in order to get protected by the Querybuilder of CI you can do this by using get_compiled_select
the following should work
$query = $this->db
->select('*')
->from('fx_dirprocess')
->where('pro_id IN ('.$this->db->select_max('pro_id')->from('fx_dirprocess')->group_by('name')->get_compiled_select().')', NULL, false)
->where('co_id', $company)
->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.