0

我有一个查询:

SELECT g.name, s.hour, gs.weekday
FROM schedule s
INNER JOIN group_schedule gs ON gs.schedule_id = s.id
INNER JOIN groups g ON g.id = gs.group_id
WHERE EXISTS (
  SELECT *
  FROM group_schedule
  WHERE group_id =6
)
ORDER BY g.name, gs.weekday

返回:

name    hour        weekday
A1-A2   10:00:00    1
A1-A2   17:00:00    1
A1-A2   10:30:00    1
A1-A2   17:30:00    1
A1-A2   18:00:00    2
A1-A2   11:30:00    2
A1-A2   18:30:00    2
A1-A2   11:00:00    2
A1-A2   12:30:00    3

但我也需要获取没有这些组合的行,例如:

name   hour      weekday
A1-A2  10:30:00  1
A1-A2  11:00:00  1
A1-A2  11:30:00  1
...

如果我使用 NOT EXISTS,我会得到一个空表。

4

1 回答 1

0

我不知道,但你的意思是这样的:

SELECT g.name, s.hour, gs.weekday
FROM schedule s
INNER JOIN group_schedule gs 
ON gs.schedule_id = s.id AND group_id = 6 
INNER JOIN groups g ON g.id = gs.group_id
ORDER BY g.name, gs.weekday

将 group_id 条件从通用 WHERE 子句中移出到 group_schedule JOIN 中?你知道在 ON 关键字后面可以有一个任意的逻辑表达式。

于 2013-03-13T17:35:15.307 回答