在此表中,一列包含2个相同的行,即courseId列。有两行是相同的,即612.现在我想编写一个查询,将该行显示为第一行,之后显示剩余的行。
我写了如下所示的查询,但它不起作用。
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;
}
答案 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;