我想获取开始时间,但它不应该在开始和结束时间之间(使用另一个表的引用获取)。我已经尝试过,但是我遇到了错误。谁能帮助我呢?
SELECT DISTINCT right(start_time,7) AS st_time
FROM Session_Info
WHERE CourseName = 'Java'
AND (right(start_time,7) NOT IN BETWEEN
(SELECT right(start_time,7)
FROM Session_Info
WHERE Session_Id IN
(SELECT Session_Id
FROM sessions
WHERE UserId='a')) AND
(SELECT right(end_time,7)
FROM Session_Info
WHERE Session_Id IN
(SELECT Session_Id
FROM sessions
WHERE UserId='a')))
同样重用相同的查询(如从UserId ='a'的会话中选择Session_Id)如何将其存储在变量中?
答案 0 :(得分:0)
您可以将代码重写为
SELECT DISTINCT right(start_time,7) AS st_time
FROM Session_Info
WHERE CourseName = 'Java'
AND Session_ID IN (SELECT Session_ID FROM sessions WHERE UserId = 'a')
AND NOT (right(start_time,7) BETWEEN right(start_time,7) AND right(end_time,7))
或使用CTE
;WITH mySessionIDs (Session_ID)
(
SELECT Session_ID
FROM sessions
WHERE UserId = 'a'
)
SELECT DISTINCT right(si.start_time,7) AS st_time
FROM Session_Info si
INNER JOIN mySessionIDs
ON si.Session_ID = mySessionIDs.Session_ID
WHERE si.CourseName = 'Java'
AND NOT (right(start_time,7) BETWEEN right(start_time,7) AND right(end_time,7))