使用内部联接和排序记录按降序排列

时间:2015-03-03 05:30:18

标签: sql

我正在使用数据库中的两个表(student_classclass)。我在下面有一个查询,显示class students。但它并不是我想要的。如何显示有学生但显示结果的课程,以便最大席位下降。需要数吗?

SELECT 
    class.class_name 
FROM 
    class 
INNER JOIN 
    student_class ON class.class_id = student_class.class_id; 

表:

Student_class

 CLASS_ID STUDENT_ID
---------- ----------
         2         12
         2         11
         2          2
         7          5
         7          6
         7          7
         7          8
         7          9
         9          2
         9         11
         9         12
        10         20
        10          2
        10          4

Class

  CLASS_ID CLASS_NAME          TEACHER_ID MAX_SEATS_AVAILABLE
---------- ------------------- ---------- -------------------
         1 Intro to ALGEBRA            11                  12
         2 Basic CALCULUS               2                  10
         3 ABC and 123                  1                  15
         4 Sharing 101                  8                  10
         5 Good Talk, Bad Talk          9                  20
         6 Nap Time                     1                  21
         7 WRITing 101                  5                  10
         8 Finger Painting              9                  14
         9 Physics 230                  2                  20
        10 Gym                          5                  25

3 个答案:

答案 0 :(得分:3)

只需使用订单声明:

SELECT class.class_name FROM class INNER JOIN student_class ON class.class_id = student_class.class_id
ORDER BY class.max_seats_available DESC

答案 1 :(得分:1)

你不需要计算。只需做一个ORDER BY MAX_SEATS_AVAILABLE DESC

SELECT class.class_name, class.max_seats_available FROM class INNER JOIN 
student_class ON class.class_id = student_class.class_id ORDER BY 
class.MAX_SEATS_AVAILABLE DESC; 

这可能会有所帮助。

答案 2 :(得分:1)

如果只需要order by MAX_SEATS_AVAILABLE试试这个:

SELECT class.class_name, class.MAX_SEATS_AVAILABLE
FROM class
INNER JOIN student_class ON class.class_id = student_class.class_id
ORDER BY class.MAX_SEATS_AVAILABLE DESC