Mysql SQL从其他表获取空字段
我是第一次与观点打交道。 我可以在视图中看到表Mysql SQL从其他表获取空字段,mysql,sql,Mysql,Sql,我是第一次与观点打交道。 我可以在视图中看到表Student中的记录,但表Course中的记录并非到处都显示为空 以下是我创建的表: -- Tabel StudentCourse maken CREATE TABLE StudentCourse ( StudentCourse int NOT NULL, CourseId int NOT NULL, StudentId int NOT NULL ) -- DROP table StudentCourse -- Tabel Course make
Student
中的记录,但表Course
中的记录并非到处都显示为空
以下是我创建的表:
-- Tabel StudentCourse maken
CREATE TABLE StudentCourse
(
StudentCourse int NOT NULL,
CourseId int NOT NULL,
StudentId int NOT NULL
)
-- DROP table StudentCourse
-- Tabel Course maken
CREATE TABLE Course
(
CourseId int NOT NULL,
Name varchar(255) NOT NULL,
StartDate date NOT NULL,
EndDate date NOT NULL,
Period int NOT NULL,
IsWeekEnd bit NOT NULL,
IsActive bit NOT NULL
)
-- DROP table Course
-- Tabel Student maken
CREATE TABLE Student
(
StudentId int NOT NULL,
Name varchar(255) NOT NULL,
Age int NOT NULL,
Address varchar(255) NOT NULL,
Registered datetime NOT NULL,
IsActive bit NOT NULL,
Description varchar(255) NOT NULL
)
-- DROP table Student
这是我创建的视图
CREATE VIEW V AS (
SELECT s.StudentId,s.Name,c.CourseId,c.StartDate FROM
Student s
LEFT JOIN
Course c
ON
s.Name=c.Name
);
谢谢你的时间 没有与学生姓名相同的课程。您需要在StudentCourse上左加入StudentCourse,在Course上左加入StudentCourse
SELECT s.StudentId,s.Name,c.CourseId,c.StartDate
FROM Student s
LEFT JOIN StudentCourse sc ON s.StudentId = sc.StudentId
INNER JOIN Course c ON sc.CourseId = c.CourseId
为什么你会期望任何学生与他们的课程同名?您的数据模型缺少
StudentCourses
表。请注意,代理PK列StudentCourse是多余的。你在剩下的列上有一个完全可用的PK。顺便说一句,还有FWIW,我认为MySQL中的视图几乎毫无意义。如果你离开一个,那么你肯定需要离开另一个!?!