当codeigniter中的Left Join表为空时,查询返回空

时间:2016-08-02 11:54:12

标签: php mysql codeigniter

我正在使用Codeigniter和MySql 7.5。我有一个查询,当左连接表为空时它总是返回空。

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

这里我在预订和商店表中都有条目,但评级表是空的。但我没有得到任何结果。

如果我从select中删除rg.rating,则会返回正确的结果。

我错过了什么吗?感谢

2 个答案:

答案 0 :(得分:2)

试试这个 -

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

答案 1 :(得分: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;

回复你的输出。还要更改config / database.php

'db_debug' => TRUE