如何编写以下查询?

时间:2017-11-21 19:27:12

标签: sql

以下输出的查询:

SELECT ce.date, p.courseExecution_date
FROM courseExecution AS ce
LEFT JOIN participant AS p
ON ce.courseID = p.courseExecution_courseID
WHERE ce.courseID = 2 and p.personID = 4;

输出:

    |     date     | courseExecution_date |
     --------------------------------------
    |  2017-12-05  |      2017-12-05      |
    |  2017-12-08  |      2017-12-05      |
    |  2017-12-09  |      2017-12-05      |
    |  2017-12-12  |      2017-12-05      |

我只想向用户显示他没有注册的日期,例如:

预期结果:

|     date     | 
 ---------------
|  2017-12-08  |
|  2017-12-09  | 
|  2017-12-12  | 

查询应该如何?非常感谢。

3 个答案:

答案 0 :(得分:0)

你的问题不是很清楚,但我想你需要这样的查询

SELECT ce.date
FROM courseExecution AS ce
WHERE NOT EXISTS
(
   SELECT 1
   FROM participant p
   WHERE ce.courseID = p.courseExecution_courseID and
         p.personID = 4 and
         p.courseExecution_date = ce.date
) and ce.courseID = 2

NOT EXISTS作为集合之间的差异(可能是你想要的)

答案 1 :(得分:0)

我假设 courseExecution_date 是用户注册的日期。 您在where语句中需要一个附加条件:

clearSearch()

答案 2 :(得分:0)

您可以在左连接中使用多个条件:

SELECT 
  ce.date, 
  p.courseExecution_date
FROM 
  courseExecution AS ce
  LEFT JOIN participant AS p
  ON ce.courseID = p.courseExecution_courseID
     and ce.courseExecution_date!=p.date
WHERE  
  ce.courseID = 2 
  and p.personID = 4;