如何将2个表连接到另一个表中

时间:2015-02-25 08:40:17

标签: mysql

这是我的表格列表:

tblsection
tblgradelevel
tblteachers

我的tblsection包含:

Section_ID | GradeID | sectionname | TeacherID

我的问题是如何在GradeID中关联TeacherIDtblsection

tblsection中的外键是:

Foreign key name   | Reference table            |  Column  | Reference Column
Section_GradeID    | 'database','tblgradelevel' | GradeID  |     GradeID
Section_TeacherID  |'database','tblteacher'     | TeacherID|   Teacher_ID

我使用许多查询来执行此问题,但它不起作用。

这是我的疑问:

"SELECT 
       a.Section_ID AS ID, 
       a.Sectionname AS SName,
       b.GradeLevel AS Level,
       c.TeacherID as Teach 
 FROM tblsection 
 INNER JOIN tblGradelevel b ON a.GradeID = b.GradeID";

我该怎么做?

2 个答案:

答案 0 :(得分:0)

**The query will generate errors due to invalid alias for the table name tblsection **
Try below query:
SELECT 
   a.Section_ID AS ID, 
   a.Sectionname AS SName,
   b.GradeLevel AS Level,
   c.TeacherID as Teach 
FROM tblsection a
INNER JOIN tblGradelevel b ON a.GradeID = b.GradeID
INNER JOIN tblteachers c   ON a.TeacherID = c.TeacherID

答案 1 :(得分:0)

$sql="select a.Section_ID, a.Sectionname, b.GradeLevel, c.TeacherID from tblsection a INNER JOIN tblgradelevel b ON a.GradeID=b.GradeID INNER JOIN tblteachers c On a.GradeID=c.TeacherID";

这两个值必须相同,例如a.gradeID=b.gradeId,然后才会执行查询。  我不会检查这个,但我正在用这种方式来处理我的应用程序。