使用多个内连接和排序结果

时间:2015-03-02 06:36:34

标签: sql

在我的数据库中有三个表。我想弄清楚如何列出学校名称,班级名称和教师姓名。按学校名称中的班级名称对记录进行排序。我有一个查询,但它无法正常工作。如何在学校名称中按类名列出学校名称,班级名称和教师姓名和分类记录?

SELECT c.class_name FROM class c INNER JOIN teacher t WHERE t.teacher_id = c.teacher_id INNER JOIN school s WHERE t.school_id = s.school_id;

TABLES

SQL> select * from 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

10 rows selected.

SQL> select * from teacher;

TEACHER_ID FIRST_NAME       LAST_NAME        T HOME_ROOM_NUM PHONE_NUM  START_DAT HO  SCHOOL_ID
---------- ---------------- ---------------- - ------------- ---------- --------- -- ----------
         1 FRanK            JOHNSON          k 10C                      22-OCT-97 In      11090
         2 LISA             JONES            h 11Bc          317-587-90 19-JAN-15 iN     123134
                                                             87

         3 Jeff             Dafferty         C W8CZ                     12-DEC-96 OH      11546
         4 Frank            MARTIN           g 12aA          212-098-98 19-JAN-15 IN      11090
                                                             76

         5 John             Smith            H 34C                      10-OCT-93 In     123134
         6 John             Smith            G 34C                      10-OCT-93 in      11090
         7 Lisa             Jones            G 11E           317-587-90 19-JAN-15 IN     123134
                                                             87

         8 Trevor           Horse            k x                        19-JAN-15 Oh      11090
         9 Gregor           Ivan             K 12A           317-987-09 10-NOV-96 KY      11090
                                                             87

        10 Gregor           Ivan             g 12A           317-987-09 10-NOV-96 Ky      11090
                                                             87

        11 Pat              Francis          H 1z1a          123-317-09 19-JAN-15 Il      11546
                                                             12

        12 Brad             Smith            G 13A                      18-NOV-94 IN      11546

12 rows selected.

SQL> select * from school;

 SCHOOL_ID SCHOOL_NAME                   SCHOOL_TYPE
---------- ----------------------------- ------------
     11546 Ivy Tech College              COLLegE
     11090 LAWRENCE Central Grade School GRADE SCHOOL
     11111 Lawrence NORTH High School    HIGH SCHooL
     19283 Howe High SCHOOL              High SchooL
    123134 Lawrence Central High School  HIGH SCHOOL
       192 Little Big Horn Grade School  GRADE SCHOOL

2 个答案:

答案 0 :(得分:1)

您可以使用join来获得所需内容。这将为所有学校提供教师和课程。

SELECT DISTINCT
   sc.SCHOOL_NAME,
   teach.FIRST_NAME,
   teach.LAST_NAME ,
   cs.CLASS_NAME
FROM 
   school sc JOIN teacher teach
      ON sc.SCHOOL_ID = teach.SCHOOL_ID
   JOIN class cs ON cs.TEACHER_ID = teach.TEACHER_ID 
ORDER BY
     sc.SCHOOL_NAME,cs.CLASS_NAME

答案 1 :(得分:0)

SELECT t.FIRST_NAME +' '+t.LAST_NAME, s.SCHOOL_NAME, c.class_name
FROM class c 
INNER JOIN teacher t 
ON t.teacher_id = c.teacher_id 
INNER JOIN school s 
ON t.school_id = s.school_id
ORDER BY  s.SCHOOL_NAME,c.CLASS_NAME