2

好的,这是我现有的查询,可以满足我的需要。

SELECT C1.id AS id, C1.title AS title, C1.instructor_id AS instructor_id, 
  C1.description AS description, C1.date AS date, C1.starttime AS starttime, 
  C1.endtime AS endtime, C1.training_id AS training_id, C2.name AS name, 
  C2.id AS instructors_id 
FROM trainings C1 
LEFT JOIN instructors C2 ON C1.instructor_id = C2.id

但是,我需要添加一些东西。除了我拥有的表之外,我还有两张表需要比较。

表“培训”有一个培训课程列表,由“id”索引。我还有另一个表“registrations”,其中包含有关哪些用户注册了哪些培训课程的信息,包含 3 列:“id”-索引,“user_id”-注册培训的用户的 ID,以及“course_id” - 培训课程的 ID。

除了我现有的查询之外,我还需要做的是仅选择在该用户的“注册”中没有一行的培训课程。

我希望这是有道理的。谢谢谁能帮忙。我已经尝试了几个小时。查询太大了,我无法保持井井有条并正确思考如何去做。

4

1 回答 1

6
SELECT C1.id AS id, C1.title AS title, C1.instructor_id AS instructor_id, 
  C1.description AS description, C1.date AS date, C1.starttime AS starttime, 
  C1.endtime AS endtime, C1.training_id AS training_id, C2.name AS name, 
  C2.id AS instructors_id 
FROM trainings C1 
LEFT JOIN instructors C2 ON C1.instructor_id = C2.id
LEFT JOIN registrations C3 ON < Whatever they're connected on >
WHERE C3.id IS NULL

基本上也是这样,并通过asJOIN过滤它会将连接表的列返回为.WHERE ... IS NULLLEFT JOINNULL

于 2012-07-23T08:59:15.800 回答