我使用下面的代码从数据库中获取数据。
$this->db->select('users.id,users.username,users.user_status,users.monthlybasepoints,Badges_benefits.nick,Badges_benefits.statusmultiplier,users.intellipoints,payment_info.minpayout,users.earning')
->where('status',3)
->from('users')
->join('Badges_benefits','Badges_benefits.id=users.user_status')
->join('payment_info','payment_info.u_id=users.id')
->limit($limit,$offset)
->order_by($orderby,$order);
$que= $this->db->get();
return $que->result_array();
它将返回下面的输出。
sr name val metal code minpayout earn
1 liz 0 zinc 120 $22 $0
2 ahs226 70 Silver 105 $50 $2.1
->join('payment_info','payment_info.u_id=users.id')
因为我在表格付款信息中只有2行,所以它只给我两个输出。而我希望它应该列出表用户的所有行,如果表格付款信息中没有任何数据,则可以将minpayout的值输出为null 像这样。
sr name val metal code minpayout earn
1 liz 0 zinc 120 $22 $0
2 ahs226 70 Silver 105 $50 $2.1
3 sdasd 34 any 343 null $34
3 sdasd 34 any 343 null $24
答案 0 :(得分:2)
使用左连接而不是内连接:
$this->db->select('users.id,users.username,users.user_status,users.monthlybasepoints,Badges_benefits.nick,Badges_benefits.statusmultiplier,users.intellipoints,payment_info.minpayout,users.earning')
->where('status',3)
->from('users')
->join('Badges_benefits','Badges_benefits.id=users.user_status', 'left')
->join('payment_info','payment_info.u_id=users.id', 'left')
->limit($limit,$offset)
->order_by($orderby,$order);
$que= $this->db->get();
return $que->result_array();