找到了很多答案,但仍然无法正常工作,这是我的查询有两个连接
$query = $this->db->select('transaction.*,restaurant.logo_url,restaurant.cover_image_url,restaurant.slug,restaurant.delivery_time,user_points.point AS earned_points')
->from('transaction')
->join('restaurant', 'transaction.restaurant_id = restaurant.id', 'left')
->join('user_points', 'transaction.order_id = user_points.transaction_order_id', 'left')
->where_in('order_id', $order_id)
->get();
return ($query->num_rows() < 1) ? null : $query->result();
获取交易表中除'restaurant'和'user_points'表之外的所有记录
连接有什么问题吗?
答案 0 :(得分:1)
试试这个
$query = $this->db->select('transaction.*,restaurant.logo_url,restaurant.cover_image_url,restaurant.slug,restaurant.delivery_time,user_points.point AS earned_points',false)
->from('transaction')
->join('restaurant', 'transaction.restaurant_id = restaurant.id', 'left')
->join('user_points', 'transaction.order_id = user_points.transaction_order_id', 'left')
->where_in('order_id', $order_id)
->get();
return $query->result();
答案 1 :(得分:0)
我强烈建议您使用以下结构。我知道它需要更多行,但更容易理解和维护。
$condition = 'condition';
$t1 = 'table1_name';
$t2 = 'table2_name';
$t3 = 'table3_name';
$get = [
$t1.'.columna',
$t1.'.columna',
$t2.'.columna',
$t2.'.columna',
$t3.'.columna',
$t3.'.columna'
]
$this->db->select($get);
$this->db->from($t1);
$this->db->join($t2, $t1.'.columna = ' . $t2.'.comlumna', 'left');
$this->db->join($t3, $t1.'.columna = ' . $t3.'.comlumna', 'left');
$this->db->where($t1.'.columna', $condition);
$result = $this->db->get()->result();