MySQL如何获取查询以显示没有客户参加的研讨会

时间:2017-11-03 20:38:29

标签: mysql sql

我是SQL新手并尝试在MySQL中为我创建的数据库编写查询。该查询旨在显示托管的每个研讨会以及参加该研讨会的每个客户的信息。该查询还旨在显示没人参加的研讨会。我怎么能这样做呢?谢谢你提前帮忙。

SELECT sem.SeminarID, sem.SeminarDate, sem.Location, sem.SeminarTitle, cust.CustomerID, cust.LastName, cust.FirstName
FROM seminar AS sem JOIN 
     seminar_customer as SC on sem.SeminarID = SC.SeminarID JOIN
     customer AS cust on SC.CustomerID = cust.CustomerID        
ORDER BY SeminarID;

1 个答案:

答案 0 :(得分:0)

你需要"外部加入"这允许返回在seminar_customer表中没有匹配行的研讨会表的某些行。

在您的查询布局方式中,seminar_customer位于" left",因此请使用" LEFT OUTER JOIN"

SELECT sem.SeminarID, sem.SeminarDate, sem.Location, sem.SeminarTitle, cust.CustomerID, cust.LastName, cust.FirstName
FROM seminar AS sem  LEFT OUTER JOIN seminar_customer as SC on sem.SeminarID = SC.SeminarID 
                     LEFT OUTER JOIN customer AS cust on SC.CustomerID = cust.CustomerID        
ORDER BY SeminarID;

请参阅:A Visual Explanation of SQL Joins

你可以省略单词" OUTER"在这些联接中,通常你会看到" LEFT JOIN"相反,但它们是相同的东西