未正确连接

qlckcl4x  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(267)

我有下面的查询,它从一系列表中获取一组列

SELECT Events.eventID, Events.eventName, Organisations.organisationName, Events.eventCrewSize, Events.eventGuestSize, Events.eventDate, Events.eventTime, Boat.boatName, Address.addressNo, Address.addressStreet, Address.addressStreetType, Address.addressSuburb, Address.addressPostCode, Address.addressState 
FROM Events, Organisations, Boat, Address 
WHERE Events.eventOrganisation = Organisations.organisationID AND Events.eventBoatID = Boat.boatID AND Events.eventAddressID = Address.addressID AND Events.eventDate >= CURDATE();

我的问题是不是所有的事件都有一个组织,而不是为该列返回null,它只是不包括任何没有组织的事件。
我尝试从where子句中删除包含空行的组织部分,但它使用另一个事件的组织作为它的值,而不是实际的空值。
我认为这是一个连接问题,但不确定如何相应地构建它。

3mpgtkmj

3mpgtkmj1#

你可以尝试使用左连接

SELECT Events.eventID, Events.eventName, Organisations.organisationName, Events.eventCrewSize, Events.eventGuestSize, Events.eventDate, Events.eventTime, Boat.boatName, Address.addressNo, Address.addressStreet, Address.addressStreetType, Address.addressSuburb, Address.addressPostCode, Address.addressState 
FROM Events 
LEFT JOIN Organisations on Events.eventOrganisation = Organisations.organisationID
LEFT JOIN Boat on Events.eventBoatID = Boat.boatID
LEFT JOIN Address on Events.eventAddressID = Address.addressID
WHERE Events.eventDate >= CURDATE();

不要忘记应用主键和外键之间的关系/规范化。你可以用 NULLIF() 而是为了避免空值

相关问题