如何将Mysqli NESTED JOIN查询转换为活动记录

时间:2019-05-02 07:48:14

标签: sql codeigniter codeigniter-3

我想将这些mysql查询转换为codeigniter Active Records

SELECT 
  u.user_id, 
  u.name as user_name, 
  u.branch, 
  b.name as branch_name, 
  r.*, 
  r3.valid_from as next_valid_from, 
  r3.valid_to as next_valid_to, 
  c.courses_name, 
  d.package_description as package_name, 
  t.from_time, 
  t.to_time, 
  u1.user_id as teacher_id, 
  u1.name as teacher_name, 
  u2.user_id as dietitian_id, 
  u2.name as dietitian_name 
FROM 
  userdetail u 
  left join branch b on u.branch = b.id 
  left JOIN (
    SELECT 
      * 
    FROM 
      rejoin 
    WHERE 
      CURDATE() BETWEEN valid_from 
      AND valid_to
  ) r on u.user_id = r.member_id 
  LEFT JOIN (
    SELECT 
      r1.* 
    FROM 
      rejoin r1 
      join (
        SELECT 
          member_id, 
          max(valid_from) as valid_from, 
          max(valid_to) as valid_to 
        FROM 
          rejoin 
        WHERE 
          CURDATE() < valid_from
      ) r2 on r1.member_id = r2.member_id 
      and r1.valid_from = r2.valid_from
  ) r3 on u.user_id = r3.member_id 
  LEFT join courses c on r.courses_id = c.courses_id 
  LEFT JOIN days_package d on r.days_type = d.package_name 
  left join timetable t on r.course_time_table_id = t.course_time_table_id 
  LEFT JOIN userdetail u1 on u.tech_assign = u1.user_id 
  left JOIN userdetail u2 on u.p_assinged_dit = u.user_id 
WHERE 
  u.usr_acc_typ = 'p' 
  and u.status = 'Active'

1 个答案:

答案 0 :(得分:0)

最后,我在M.Hemant的帮助下得到了答案

$this->db->select('u.user_id,
                  u.name as user_name,
                  u.branch as branch_id,
                  b.name as branch_name, 
                  r.courses_id,c.courses_name, 
                  r.days_type,d.package_description as package_name,
                  r.valid_from, 
                  r.valid_to, 
                  r1.valid_from as next_valid_from,
                  r1.valid_to as next_valid_to,
                  r.course_time_table_id,
                  t.from_time,
                  t.to_time,
                  u1.user_id as teacher_id,
                  u1.name as teacher_name,
                  u2.user_id as dietitian_id, 
                  u2.name as dietitian_name,
                  p.gender,
                  u.status')
        ->from('userdetail u')
        ->join('branch b','b.id = u.branch','LEFT')
        ->join("(SELECT * FROM rejoin WHERE '{$today}' BETWEEN valid_from AND valid_to  ) as r ",'r.member_id = u.user_id','LEFT')
        ->join("(SELECT * FROM rejoin WHERE '{$today}' < valid_from AND valid_to  ) as r1 ",'r1.member_id = u.user_id','LEFT')
        ->join('courses c','c.courses_id = r.courses_id','LEFT')
        ->join('days_package d', 'd.package_name = r.days_type','LEFT')
        ->join('timetable t','t.course_time_table_id =r.course_time_table_id','LEFT')
         ->join('personaldetail p','p.user_id = u.user_id','LEFT')
         ->join('userdetail u1','u1.user_id = u.tech_assign','LEFT')
         ->join('userdetail u2','u2.user_id = u.p_assinged_dit','LEFT')
         ->where('u.usr_acc_typ','p')
         ->where('u.status','Active')
         ->get();