简体   繁体   English

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

[英]MySQL How to Get Query To Display Seminars With No Customers Attending

I am new to SQL and trying to write a query in MySQL for a database I have created. 我是SQL的新手,正尝试在MySQL中为已创建的数据库编写查询。 The query is meant to display each seminar that is hosted and the information of each customer attending it. 该查询旨在显示所主持的每个研讨会以及参加该研讨会的每个客户的信息。 The query is also meant to display the seminars which nobody attends. 该查询还用于显示没有人参加的研讨会。 How would I get it to do this? 我将如何做到这一点? Thank you for any help ahead of time. 感谢您提前提供的帮助。

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;

You require an "OUTER JOIN" which allows some rows of your seminar table to be returned that have no matching row in the seminar_customer table. 您需要一个“外部联接”,以允许返回研讨会表的某些行,而该行在workshop_customer表中没有匹配的行。

In the way your query is laid out seminar_customer is on the "left", so use a "LEFT OUTER JOIN" 按照您的查询布局方式,“ courses_customer”位于“左侧”,因此请使用“ 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;

See: A Visual Explanation of SQL Joins 请参阅: SQL连接的直观说明

You can omit the word "OUTER" in those joins, so often you will just see "LEFT JOIN" instead, but they are the same things 您可以在那些联接中省略“ OUTER”一词,因此通常您只会看到“ LEFT JOIN”,但是它们是相同的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM