一列有2个相同的行,我想先显示它

时间:2018-01-11 08:34:19

标签: mysql sql

在此表中,一列包含2个相同的行,即courseId列。有两行是相同的,即612.现在我想编写一个查询,将该行显示为第一行,之后显示剩余的行。

course_mapping_table

enter image description here

课程表

enter image description here

用户

enter image description here

我写了如下所示的查询,但它不起作用。

public function gettrendingcourse(){
    $sql="SELECT * FROM
    course c
    INNER JOIN course_student_mapping cm
    ON cm.courseId = c.courseId
    INNER JOIN users u
    ON c.userId = u.userId
    INNER JOIN category s
    ON s.categoryId=c.categoryId
    GROUP BY c.courseId HAVING count(*) >= 1
    ORDER BY cm.courseId DESC ";
    $query=$this->db->query($sql);

    return $query;  
}

2 个答案:

答案 0 :(得分:0)

INNER JOIN用户u     ON c.userId = u.userId - 这应该抛出一个错误,当然没有可见的用户标识,我怀疑连接应该在cm.studentid上。

答案 1 :(得分:0)

ORDER BY子句中使用子查询,以便按学生人数订购课程:

SELECT * 
FROM course c
JOIN course_student_mapping cm ON cm.courseId = c.courseId
JOIN users u ON u.userId = cm.studentid
JOIN category s ON s.categoryId = c.categoryId
ORDER BY 
(
  select count(*) 
  from course_student_mapping cm2
  where cm2.courseid = c.courseid
) DESC, courseid, studentid;