Dev*_*vin 2 mysql select compare 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
Run Code Online (Sandbox Code Playgroud)
但是,我需要添加一些东西.除了我所拥有的,我还需要比较两个表.
表'培训'有一个培训课程列表,用'id'索引.我还有另一个表"注册",其中包含用户注册哪些培训课程有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
Run Code Online (Sandbox Code Playgroud)
Basicly仅仅JOIN是藏汉,并通过筛选它WHERE ... IS NULL,因为LEFT JOIN将返回连接表的列作为NULL.