我如何用这个3表制作视图?

时间:2012-04-09 08:28:22

标签: mysql database

我有三张桌子。设计就像

学生表

create table student (studID int not null primary key AUTO_INCREMENT,
StudName varchar(20),
Parent varchar(20),
PhoneNo int not null
)

课程表设计

create table Course (CID int not null primary key AUTO_INCREMENT,
CName varchar(20))

studCourse表设计

create table studCourse(studID int not null
,CID int not null
)

我如何制作一个显示学生姓名和他正在学习的课程的视图?

2 个答案:

答案 0 :(得分:0)

  CREATE VIEW vwStudent  AS
       SELECT
      s.StudName,
      c.CName
    FROM student s
      INNER JOIN studCourse sc
        ON s.studID = sc.studID
      INNER JOIN Course c
        ON c.CID = sc.CID

 CREATE VIEW vwStudent  AS

    SELECT
          s.StudName,
          c.CName
        FROM student s
          JOIN studCourse sc
            ON s.studID = sc.studID
          JOIN Course c
            ON c.CID = sc.CID

试试这个

答案 1 :(得分:0)

您可以使用JOIN从查询创建一个视图,这样的事情应该起作用:

CREATE VIEW v AS (
SELECT s.StudName AS student,c.CName AS course  
FROM student s
JOIN studCourse d USING(studID)
JOIN Course c ON (d.CID = c.CID)
)