MySQL-LEFT JOIN并从不在另一个表中的表中选择行



好的,这里有一个现有的查询,可以满足我的需求。

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

然而,我需要补充一些东西。除了我现有的表之外,我还有两张表需要比较。

表"trainings"有一个培训课程列表,按"id"进行索引。我还有另一个表"registrations",它包含关于哪些用户注册了哪些培训课程的信息,共有3列:"id"-索引,"user_id"-注册培训的用户的id,"course_id"–培训课程的id。

除了我现有的查询之外,我需要做的是只选择"注册"中没有该用户行的培训课程。

我希望这是有道理的。谢谢你的帮助。我已经试了好几个小时了。这个查询太大了,我无法保持组织并正确思考如何进行。

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

基本上也只使用JOIN,并通过WHERE ... IS NULL进行过滤,因为LEFT JOIN将以NULL的形式返回联接表的列。

相关内容

  • 没有找到相关文章

最新更新