我正在尝试使用CodeIgnitor活动记录从三个表中获取结果。但我只是得到了与第二次加入条件匹配的记录。但我需要所有记录......
$uid = $this->session->userdata('uid');
$codn = ['stud.uid'=>$uid,'stud.status'=>"ACTIVE"];
$liststudent = $this->db->select('stud.id,stud.studname,stud.mobile1,stud.photo,stud.totalfees,bs.title,SUM(sp.pay_in_digit) AS paidfees')
->from('students as stud')
->join('batchsetting as bs', 'bs.id = stud.bid')
->join('stud_payment as sp', 'sp.sid = stud.id')
->where($codn)
->get();
return $liststudent->result();
这是我的查询。
概念是:我需要批量设置的批量标题,他们是谁的id = stud.bid'。来自stud_payment的payfees和' sid = stud.id'。
但结果我只得到那些在stud_payment表中记录的学生记录。
答案 0 :(得分:0)
表students
没有任何名为bid
的字段。所以
更改
->join('batchsetting as bs', 'bs.id = stud.bid')
以强>
->join('batchsetting as bs', 'bs.id = stud.id')
答案 1 :(得分:0)
尝试使用以下代码
$uid = $this->session->userdata('uid');
$liststudent = $this->db->select('stud.id,stud.studname,stud.mobile1,stud.photo,stud.totalfees,bs.title,SUM(sp.pay_in_digit) AS paidfees')
->from('students as stud')
->join('batchsetting as bs', 'bs.id = stud.id')
->join('stud_payment as sp', 'sp.sid = stud.id')
->where('stud.uid',$uid)
->where('stud.status',"ACTIVE");
return $liststudent->result();
答案 2 :(得分:0)
尝试在您的关节中使用左连接。代码如下:
$uid = $this->session->userdata('uid');
$codn = ['stud.uid'=>$uid,'stud.status'=>"ACTIVE"];
$liststudent = $this->db->select('stud.id,stud.studname,stud.mobile1,stud.photo,stud.totalfees,bs.title,SUM(sp.pay_in_digit) AS paidfees')
->from('students as stud')
->join('batchsetting as bs', 'bs.id = stud.bid', 'left')
->join('stud_payment as sp', 'sp.sid = stud.id', 'left')
->where($codn)
->get();
return $liststudent->result();
答案 3 :(得分:0)
基本上ci bydefault考虑内部Join查询。所以你需要指定左连接
->join('stud_payment as sp', 'sp.sid = stud.id', 'left');
答案 4 :(得分:0)
使用全外连接。
function full_outer_join() {
$sql = 'SELECT ' . $this->blogs . '.blog_id,comment_id,blog_title,blog_content,blog_date,comment_text,comment_date
FROM ' . $this->blogs . ' LEFT OUTER JOIN ' . $this->blog_comments . ' ON ' . $this->blog_comments . '.blog_id = ' . $this->blogs . '.blog_id
UNION
SELECT ' . $this->blogs . '.blog_id,comment_id,blog_title,blog_content,blog_date,comment_text,comment_date
FROM ' . $this->blogs . ' RIGHT OUTER JOIN ' . $this->blog_comments . ' ON ' . $this->blog_comments . '.blog_id = ' . $this->blogs . '.blog_id';
$query = $this->db->query($sql);
return $query->result();
}