我有这两个问题。
查询1:
SELECT fex.availableID, fex.lecturerID, fex.freedate,
ex.lecturerID AS Examiner, ex.lecturerFullname,
v.availableID, v.availableDay, v.availableStart, v.availableEnd, v.availableEnd
FROM free fex
INNER JOIN lecturer ex
ON fex.lecturerID = ex.lecturerID
INNER JOIN availability v
ON fex.availableID = v.availableID
查询2:
SELECT s.studentID, s.studentName, s.projectTitle, s.lecturerID AS supervisor,
sv.lecturerID, sv.lecturerFullname,
fsv.availableid, fsv.lecturerid, fsv.freedate
FROM student s
INNER JOIN lecturer sv
ON s.lecturerID = sv.lecturerID
INNER JOIN free fsv
ON s.lecturerID = fsv.lecturerID
正如您所见,讲师在这里扮演两个角色supervisor
和examiner
。查询1显示examiner
的查询。查询2显示supervisor
及其学生的查询。
数据库方案:
免:
availableID (number)
lecturerID (varchar2)
freedate (Date)
讲师:
lecturerID (varchar2)
lecturerFullname (varchar2)
状况:
availableID (number)
availableDay (varchar2)
availableStart (Date) //Time
availableEnd (Date) //Time
availableDate (Date)
学生:
studentID (varchar2)
studentName (varchar2)
Projecttitle (varchar2)
lecturerID(varchar2)
首先,我想加入这两个问题。因为他们没有关系。我尝试过使用CROSS JOIN
,但我得到了无穷大的结果。
其次,查询加入后。我希望查询能够找到supervisor
或examiner
,其中一个在freedate
中有价值(例如:10-05-2016)。由于它们中的大多数在freedate
中都具有NULL值。只有某些讲师才有价值。
然后,它将生成一个查询结果,其中显示如下内容:
studentID studentName projectTitle supervisor examiner availableID availableDay freeDate
123 hunter abc mary kent 10 Tuesday 10-05-2016
例如,审查员有freedate
10-05-2016。同时,主管的空值为freedate
。
或者也许有任何有效的方法?
答案 0 :(得分:0)
类似
SELECT fex.availableID, fex.lecturerID, fex.freedate,
ex.lecturerID AS Examiner, ex.lecturerFullname,
v.availableID, v.availableDay, v.availableStart, v.availableEnd, v.availableEnd
FROM free fex
INNER JOIN lecturer ex
ON fex.lecturerID = ex.lecturerID
INNER JOIN availability v
ON fex.availableID = v.availableID
CROSS APPLY
(
SELECT s.studentID, s.studentName, s.projectTitle, s.lecturerID AS supervisor,
sv.lecturerID, sv.lecturerFullname,
fsv.availableid, fsv.lecturerid, fsv.freedate
FROM student s
INNER JOIN lecturer sv
ON s.lecturerID = sv.lecturerID
INNER JOIN free fsv
ON s.lecturerID = fsv.lecturerID
WHERE sv.lecturerID = ex.lecturerID
) DQ
答案 1 :(得分:0)
正如我从数据库表中看到的,所有都有一个共同的密钥可以加入。
数据库方案:
免:
availableID(number)讲授(varchar2)freedate(日期)
讲师:
讲授(varchar2)讲师全名(varchar2)
状况:
availableID(number)availableDay(varchar2)availableStart(Date) availableEnd(Date)// Time availableDate(Date)
学生:
studentID(varchar2)studentName(varchar2)Projecttitle(varchar2) lecturerID(VARCHAR2)
SELECT *
FROM FREE FR, LECTURER LR, AVAILABILITY AV, STUDENT ST
WHERE FR.AVAILABLEID = AV.AVAILABLEID
AND LR.LECTURERID = FR.LECTURERID
AND ST.LECTURERID = FR.LECTUREID
但是,既然你已经提到两者不同,你可以使用UNION。
SELECT fex.availableID, fex.lecturerID, fex.freedate,
ex.lecturerID AS Examiner, ex.lecturerFullname,
v.availableID, v.availableDay, v.availableStart, v.availableEnd, v.availableEnd
FROM free fex
INNER JOIN lecturer ex
ON fex.lecturerID = ex.lecturerID
INNER JOIN availability v
ON fex.availableID = v.availableID
UNION
SELECT s.studentID, s.studentName, s.projectTitle, s.lecturerID AS supervisor,
sv.lecturerID, sv.lecturerFullname,
fsv.availableid, fsv.lecturerid, fsv.freedate
FROM student s
INNER JOIN lecturer sv
ON s.lecturerID = sv.lecturerID
INNER JOIN free fsv
ON s.lecturerID = fsv.lecturerID
UNION运算符默认情况下仅选择不同的值。要允许重复值,请将UN关键字与ALL关键字一起使用。