将SQL查询转换为CodeIgniter查询

时间:2019-03-06 01:58:53

标签: php sql-server codeigniter

我在SQL Server中为该查询找到可靠的解决方案时遇到了麻烦,现在有人可以帮助我将此查询转换为codeigniter查询,任何帮助将不胜感激

SELECT DISTINCT
    s1.idnumber,
    s1.firstname,
    s1.middlename,
    s1.lastname,
    (
        SELECT DISTINCT 
            SUM(s4.point) 
        FROM students s3 
        LEFT JOIN section_student s4 
        ON s3.id = s4.student_id
        WHERE s3.id = s1.id AND s4.schoolyear = s2.schoolyear AND s4.semester = s2.semester
    ) AS 'point'
FROM
students s1
LEFT JOIN section_student s2
ON s1.id = s2.student_id
WHERE s2.schoolyear = '2018-2019' AND s2.semester = '2nd'
GO

*已编辑

this is the result of my query

and the structure of my database

尝试检索2018-2019学年和第二学期之内的所有数据

1 个答案:

答案 0 :(得分:0)

尝试一下

$this->db->select(" DISTINCT s1.idnumber,s1.firstname,s1.middlename,s1.lastname,(SELECT DISTINCT  SUM(s4.point)  FROM students s3  LEFT JOIN section_student s4  ON s3.id = s4.student_id WHERE s3.id = s1.id AND s4.schoolyear = s2.schoolyear AND s4.semester = s2.semester) AS point");
  $this->db->from('students s1');
  $this->db->join('section_student s2','s1.id = s2.student_id','left');
  $this->db->where('s2.schoolyear','2018-2019');
  $this->db->where('s2.semester','2nd');
  $query = $this->db->get();
  echo $this->db->last_query();die;//it will give you query please copy this and run in your db
  if ($query->num_rows() > 0) {
       return $query->result();
  } else {
       return array();
  }