一列有两行,我想先显示它

zsohkypk  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(403)

在该表中,一列包含两个相同的行,即courseid列。有两排和612排一样。现在,我想编写一个查询,将该行显示为第一行和之后的剩余行。

课程Map表

课程表

用户


我写了下面这样的查询,但它不工作。

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;  
}
zaq34kh6

zaq34kh61#

inner join users u on c.userid=u.userid-这会引发一个错误,课程中没有可见的userid,我怀疑连接应该在cm.studentid上。

thtygnil

thtygnil2#

在中使用子查询 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;

相关问题