SQL Server查询:使用JOIN包含NULL值

前端之家收集整理的这篇文章主要介绍了SQL Server查询:使用JOIN包含NULL值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要有关以下sql Server查询的帮助,其中a.TAProfileID和c.CountryCode列在数据库中具有“NULL”值.

我希望我的JOIN语句返回存在的“NULL”值.

SELECT 
a.ReservationStayID AS 'Reservation Id',a.PMSConfirmationNumber as 'PMS No',a.CreatedOn AS 'Date Created',a.ArrivalDate AS 'Date of Arrival',a.DepartureDate AS 'Date of Departure',a.TAProfileID AS 'TA Id',a.StatusCode AS 'Status',b.PropertyCode AS 'Hotel',c.Name AS 'Travel Agency',c.CountryCode AS 'Market Code',d.CountryName AS 'Mkt'

FROM ReservationStay a

inner JOIN GuestStaySummary b ON a.ReservationStayID = b.ReservationStayID
inner JOIN TravelAgency c ON a.TAProfileID = c.TravelAgencyID
inner JOIN Market d ON c.CountryCode = d.CountryCode

解决方法

要返回或生成NULL值,您必须使用LEFT JOIN.

所以,你的查询应该是这样的:

SELECT 
     a.ReservationStayID AS 'Reservation Id',a.PMSConfirmationNumber AS 'PMS No',c.NAME AS 'Travel Agency',d.CountryName AS 'Mkt'
FROM ReservationStay a
    INNER JOIN GuestStaySummary b ON a.ReservationStayID = b.ReservationStayID
    LEFT JOIN TravelAgency c ON a.TAProfileID = c.TravelAgencyID
    LEFT JOIN Market d ON c.CountryCode = d.CountryCode
原文链接:https://www.f2er.com/mssql/77222.html

猜你在找的MsSQL相关文章