Stuti Rauthan
Stuti Rauthan

Reputation: 39

Distinct with join in codeigniter mysql

How can I use Distinct with join in CodeIgniter, I'm trying to fetch customer's name and registered category, but my problem is whiles fetching the data I'm getting data in repetation because of sub-category, I've used distinct but then I'm getting Ambiguous field error, how can I solve this problem. My View

<table class="table table-hover example1">
<thead>
<tr>
    <th>SNo.</th> 
    <th>Business Name</th>
    <th>Category Name</th>
    <th>Edit</th>
</tr> 
</thead>
<tbody>
 <?php $i=0;
 foreach($value as $row){ ?>
 <tr>
    <td><?php echo ++$i;?></td>
    <td><?php echo $row->Bussiness_Name;?></td>
    <td><?php echo $row->Category_Name;?></td>
    <td onClick="RestoreVendor(<?php echo $row->Business_Id; ?>,<?php echo $row->Trash;?>,'<?php echo $row->Bussiness_Name;?>')" class="btn btn-primary">Remove</td>
 </tr>
 <?php }?>
 </tbody>
 </table>

My Controller:

 public function removecategory()
 {
    $this->load->model('VendorModel');
    $data = $this->VendorModel->remove_category();
    $this->load->view('admin/remove_cat', array('value'=>$data));
 }

My Model

 public function remove_category()
{
    $this->db->select('*');
    $this->db->distinct('Category_Id');
    $this->db->from('business_mapping');
    $this->db->join('business_profile_details', 'business_profile_details.Business_Id = business_mapping.Business_Id');
    $this->db->join('category_master', 'category_master.Category_Id = business_mapping.Category_Id');
    $query = $this->db->get();
    return $query->result();
}

result image

enter image description here

Upvotes: 1

Views: 5338

Answers (2)

Alex Mac
Alex Mac

Reputation: 2993

You can use below mentioned query.

$query = $this->db->group_by('category_master.Category_Id,business_profile_details.Business_Id');

Please try above, It will help you.

Upvotes: 1

Yadhu Babu
Yadhu Babu

Reputation: 1521

you can use

$this->db->distinct();

Some times distinct not works then you can use

$this->db->group_by('business_mapping.unique_coloumn_Name');

Upvotes: 0

Related Questions