0

I'm using Codeigniter with MySql 7.5. I have a query and it always returns empty when left join table is empty.

$this->db->select('shop.id as shop_id, shop.shop_name, rg.rating');
$this->db->from('shop');            
$this->db->join('booking as bh', 'bh.shop_id = shop.id', 'left');
$this->db->join('rating rg', 'rg.booking_id = bh.id', 'left');
$this->db->group_by("bh.id");
$this->db->order_by("bh.id", "desc");

Here I have entries in booking and shop tables but rating table is empty. But I didn't get any results.

If I remove rg.rating from select it will return correct result.

Did I miss anything? Thanks

5
  • 1
    I have no idea about CodeIgniter, but from the look of your query I think the way you are using group by seems to be wrong Commented Aug 2, 2016 at 11:57
  • Thanks but I didn't get results even I remove group_by Commented Aug 2, 2016 at 12:01
  • I suspect it is your group by that is causing the empty result set. You are grouping on a result that might not be there. You also aren't including the group by column in the select statement. My suggestion would be to turn on the Profiler and see what the actual query looks like. Commented Aug 2, 2016 at 13:03
  • @RaGu is this field is correct 'rg.booking_id = bh.id' ?? bh.id Commented Aug 2, 2016 at 15:09
  • you used group_by with left joined table hence you don't get any result Commented Aug 4, 2016 at 21:44

2 Answers 2

2

Try this-

$this->db->select('shop.id as shop_id, shop.shop_name, rating.rating');
$this->db->join('booking', 'booking.shop_id = shop.id', 'left');
$this->db->join('rating', 'rating.booking_id = booking.id', 'left');
$this->db->group_by("booking.id");
$this->db->order_by("booking.id", "desc");
return $this->db->get('shop');
Sign up to request clarification or add additional context in comments.

1 Comment

All you need to apply 'group by' properly.@Ragu
0
$this->db->select('s.id as shop_id, s.shop_name, r.rating');

$this->db->from('shop s');            

$this->db->join('booking bh', 'bh.shop_id = shop.id');

$this->db->join('rating r', 'r.booking_id = bh.id');

$this->db->group_by("bh.id");

$this->db->order_by("bh.id", "desc");

   $qry =  $this->db->get();

   echo $this->db->last_query();

   print_r($qry->result_array()); exit;

Reply your output. Also change config/database.php

'db_debug' => TRUE

1 Comment

No. I got empty results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.