随机选择oracle

时间:2016-06-18 05:34:53

标签: sql oracle oracle11g

我有这个问题:

SELECT s.studentID,   s.studentName, s.projectTitle, s.lecturerID,
sv.lecturerID,sv.lecturerFullname AS supervisor,
ex.lecturerID,ex.lecturerfullname AS examiner,
f.availableID, f.lecturerID, f.freedate,
v.availableID, v.availableday, v.availablestart, 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 = ? 
AND s.lecturerID = ?
AND s.studentID = ?
AND f.freedate IS NOT NULL

此查询用于查找examiner非NOT NULL的freedate并指定可用日期。

例如free表:

freeid  lecturerID availableID   freedate
2451    1004           6        11-05-2016 
2461    1002           6        11-05-2016 
2471    1001           21       17-06-2016 
2481    1001           8        27-06-2016 

讲师可以是examinersupervisor。在此查询中,它专门用于examiner。因此,假设lecturerID表中的free适用于examiner

正如您所看到的,lecturerID = 1001有两个freedate。 但问题是,插入的日期始终只有17-06-2016

如果一位审查员有两个或更多freedate,我希望查询更灵活,它会随机选择一位讲师(审查员)freedate

每个日期只能使用四次(如果可能)..

当前结果(我删除了无关列):

StudentID   StudentName     availableDay         freeDate
  2013         abc             Friday           17-06-2016
  2014         def             Friday           17-06-2016
  2015         ghi             Friday           17-06-2016
  2016         klm             Friday           17-06-2016
  2010         nop             Friday           17-06-2016

预期结果:

StudentID   StudentName     availableDay         freeDate
  2013         abc             Friday           17-06-2016
  2014         def             Monday           27-06-2016
  2015         ghi             Monday           27-06-2016
  2016         klm             Friday           17-06-2016
  2010         nop             Friday           17-06-2016

1 个答案:

答案 0 :(得分:0)

看起来你现在正在交叉加入Sub Query而没有select语句或表别名。

CROSS JOIN (free f
    INNER JOIN lecturer ex
    ON f.lecturerID = ex.lecturerID
    INNER JOIN availability v
    ON f.availableID = v.availableID)

你真的想要cross join吗? free的edata结构是什么?您希望的所有日期都包含在free表中,因为它看起来是相关列的来源。并且inner joinleft join会对免费表格更有效吗?