将不相关的表与不同的列号组合在一起(oracle 11g)

时间:2016-05-23 00:41:47

标签: oracle oracle11g

首先查询:

SELECT f.availableID, f.lecturerID,
ex.lecturerID AS Examiner, ex.lecturerFullname,
v.availableID, v.availableDay, v.availableStart, v.availableEnd, v.availableEnd
FROM free f
INNER JOIN lecturer ex
ON f.lecturerID = ex.lecturerID
INNER JOIN availability v
ON f.availableID = v.availableID

第二次查询:

SELECT s.studentID, s.studentName, s.projectTitle, s.lecturerID AS supervisor,
sv.lecturerID, sv.lecturerFullname
FROM student s 
INNER JOIN lecturer sv
ON s.lecturerID = sv.lecturerID

我想结合这两个查询。所以我尝试UNION但由于列数不相等而无效。另外,这两个查询都有INNER JOIN,这让我更加困惑。

我想使用ex.availableID来查询这些信息。

此查询旨在找到审核员availableID,但我需要加入studentsupervisor,彼此之间不相关。

预期结果(这些是我想要的重要栏目,我忽略了另一个重复的栏目以便于理解):

studentID   studentName   projectTitle   supervisor   examiner   availableID
  123          hunter         abc           mary        kent        10

数据库方案:

免:

availableID (number)
lecturerID (varchar2)

讲师:

lecturerID (varchar2)
lecturerFullname (varchar2)

状况:

availableID (number)
availableDay (varchar2)
availableStart (Date) //Time
availableEnd (Date)   //Time
availableDate (Date)

学生:

studentID (varchar2)
studentName (varchar2)
Projecttitle (varchar2)
lecturerID(varchar2)
在我的案例中,

lecturer表将用作supervisorexaminer两次。如果我可以将它们组合成一个选择语句,那将会很好:)

更新 我根据要求稍微更新了我的查询:

SELECT s.studentID,   s.studentName, s.projectTitle, sv.lecturerFullname AS supervisor,
ex.lecturerfullname AS examiner, f.availableID, f.lecturerID, 
v.availableID, v.availableday, v.availablestart, v.availableend, v.availableend
FROM student s
INNER JOIN lecturer sv
  ON s.lecturerID = sv.lecturerID
CROSS JOIN (free f
  INNER JOIN lecturer ex
    ON f.lecturerID = ex.lecturerID
  INNER JOIN availability v
    ON f.availableID = v.availableID)
WHERE f.lecturerID = '1009'
AND s.lecturerID = '1027'
AND s.studentID = '2013366609'; 

它在db中运行良好..但是正如我在JAVA中使用它,它给了我一个例外: java.sql.SQLException: ORA-06553: PLS-306: wrong number or types of arguments in call to 'V'

1 个答案:

答案 0 :(得分:0)

UNION运算符不是您需要的。正如你所说,你发布的两个查询之间没有任何关系,也许CROSS JOIN会得到你所需要的。但是,如果表格很大,您可能需要添加一些条件来过滤结果。它应该是这样的:

SELECT s.studentID,   s.studentName, s.projectTitle, sv.lecturerFullname AS supervisor, ex.lecturer AS examiner, f.availableID
FROM student s
INNER JOIN lecturer sv
  ON s.lecturerID = sv.lecturerID
CROSS JOIN (free f
  INNER JOIN lecturer ex
    ON f.lecturerID = ex.lecturerID
  INNER JOIN availability v
    ON f.availableID = v.availableID)
WHERE f.availableID = 10;