简体   繁体   中英

Query return empty when Left Join table is empty in codeigniter

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

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');
$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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM