SQL Join - 如果连接表不包含任何数据,则显示数据

时间:2015-09-17 12:31:27

标签: sql join

我有一个将3个表连接在一起的查询。 一个是SPAAPPOINTMENTS表 - (价格收费的水疗中心预约) 第二个是SPAAPTADDONS - 约会的插件(Nibbles等),apts和rst之间的链接 第三个是RSTCATALOG表 - 插件价格的位置。

查询:

SELECT        SPAAPPOINTMENTS.APTID, SPAAPPOINTMENTS.SERVICECODE, SPAAPPOINTMENTS.LOCATIONCODE, SPAAPPOINTMENTS.STAFFCODE, 
                         SPAAPPOINTMENTS.BLOCKSTARTTIME, SPAAPPOINTMENTS.BLOCKENDTIME, SPAAPPOINTMENTS.I_RECID, SPAAPPOINTMENTS.S_STAYID, 
                         SPAAPPOINTMENTS.STATUSCODE, SPAAPPOINTMENTS.BLOCKEDTIME, SPAAPPOINTMENTS.GRATUITY, SPAAPPOINTMENTS.TEMPHOLDID, 
                         SPAAPPOINTMENTS.LASTCHGID, SPAAPPOINTMENTS.CANCELID, SPAAPPOINTMENTS.ITEMNO, SPAAPPOINTMENTS.COMMENTS, 
                         SPAAPPOINTMENTS.OFFSITELOCATION, SPAAPPOINTMENTS.PRICE, SPAAPPOINTMENTS.PRIORITY, SPAAPPOINTMENTS.SPAPKGCODE, 
                         SPAAPPOINTMENTS.ENFORCEGENDER, SPAAPPOINTMENTS.NEWSTATUS, SPAAPPOINTMENTS.RESTNO, SPAAPPOINTMENTS.DONOTMOVE, 
                         SPAAPPOINTMENTS.NOSHOW, SPAAPPOINTMENTS.OTHERGENDERREQUESTED, SPAAPPOINTMENTS.MADEBY, SPAAPPOINTMENTS.LINKCODE, 
                         SPAAPPOINTMENTS.CONFIRMED, SPAAPPOINTMENTS.CXLNUMBER, SPAAPPOINTMENTS.REQUEST, SPAAPPOINTMENTS.CUSTOMFIELD1, 
                         SPAAPPOINTMENTS.CUSTOMFIELD2, SPAAPPOINTMENTS.CUSTOMFIELD3, SPAAPPOINTMENTS.VIP, SPAAPPOINTMENTS.NOPREFERENCE, 
                         SPAAPPOINTMENTS.CUSTOMFIELD4, SPAAPPOINTMENTS.CUSTOMFIELD5, SPAAPPOINTMENTS.BOOKID, SPAAPPOINTMENTS.ARTSRESVNO, 
                         SPAAPPOINTMENTS.LMSCONFNO, SPAAPPOINTMENTS.ARTSGUESTID, SPAAPPOINTMENTS.CLUBNO, SPAAPPOINTMENTS.MULTIGRPID, 
                         SPAAPPOINTMENTS.MAINCONTACT, SPAAPPOINTMENTS.SPABKNAME, SPAAPPOINTMENTS.MULTIGRPAPPTXT, SPAAPPOINTMENTS.CONFCOMMENTS, 
                         SPAAPPOINTMENTS.MEMCODE, SPAAPPOINTMENTS.FAMILYMEMTYPE, SPAAPPOINTMENTS.COCOMMENTS, SPAAPPOINTMENTS.GRPMASTERID, 
                         SPAAPPOINTMENTS.GUESTTYPE, SPAAPPOINTMENTS.EXTUNIQUEID, SPAAPPOINTMENTS.SETUPTIME, SPAAPPOINTMENTS.BREAKDOWNTIME, 
                         SPAAPPOINTMENTS.PSBASEPRICE, SPAAPPOINTMENTS.PSCODE, SPAAPPOINTMENTS.PSCALENDARADJPCT, SPAAPPOINTMENTS.PSDETAILADJ, 
                         SPAAPPOINTMENTS.PSPRICE, SPAAPPOINTMENTS.PSDETAILADJSOURCE, SPAAPPOINTMENTS.PSDETAILADJTYPE, SPAAPPOINTMENTS.PSPRICETYPE, 
                         RSTCATALOG.C_DPRICE
FROM  SPAAPPOINTMENTS 
  INNER JOIN SPAAPPTADDONS ON SPAAPPOINTMENTS.APTID = SPAAPPTADDONS.APTID 
  INNER JOIN RSTCATALOG ON SPAAPPTADDONS.ITEMNO = RSTCATALOG.C_ITEM

这将从约会表中返回所有数据,因为他们已经加入了水疗中心预约。

如果他们刚刚预约,并且没有添加,则不会返回数据。 例如,如果我使用查询并将其后缀为:WHERE (SPAAPPOINTMENTS.APTID = 626746)

它没有显示任何数据,但在spaappopintments中有一行但在插件表中没有任何数据。

如果我使用anopther AptID,它有HAD ANAD ADDON,它会显示数据并从查询中输出。

如果SPAAPTADDONS表中没有数据,我怎样才能显示数据呢?

3 个答案:

答案 0 :(得分:1)

更改以下INNER JOIN

INNER JOIN SPAAPPTADDONS ON SPAAPPOINTMENTS.APTID = SPAAPPTADDONS.APTID
INNER JOIN RSTCATALOG ON SPAAPPTADDONS.ITEMNO = RSTCATALOG.C_ITEM 

LEFT OUTER JOIN喜欢

LEFT JOIN SPAAPPTADDONS ON SPAAPPOINTMENTS.APTID = SPAAPPTADDONS.APTID
LEFT JOIN RSTCATALOG ON SPAAPPTADDONS.ITEMNO = RSTCATALOG.C_ITEM 

答案 1 :(得分:1)

FROM  SPAAPPOINTMENTS 
LEFT JOIN SPAAPPTADDONS ON SPAAPPOINTMENTS.APTID = SPAAPPTADDONS.APTID 
LEFT JOIN RSTCATALOG ON SPAAPPTADDONS.ITEMNO = RSTCATALOG.C_ITEM

让SPAAPPOINTMENTS的所有数据都在结果中。

答案 2 :(得分:1)

将表连接在一起时,请指定 left right 表。 inner join将生成一个集合,其中左侧和右侧的连接条件均为真。

你想要的是获得一个包含 left 表中所有数据的集合以及来自 right 一侧的匹配数据。这是left join

但是,由于您还希望获得插件的定价信息,您还需要加入第三个表。我们可以假设插件和定价表之间存在一对一匹配,看起来我们可以在这里使用inner join,但这不会起作用,因为连接是按顺序执行的,这意味着最后一个连接将使用第一个连接的结果作为其左侧(并且该组将具有没有任何插件的约会的空值) - 如果我们尝试使用定价表执行inner join那些行将是排除在外,所以你在这里也需要left join

最后你想要这个:

FROM SPAAPPOINTMENTS 
LEFT JOIN SPAAPPTADDONS ON SPAAPPOINTMENTS.APTID = SPAAPPTADDONS.APTID 
LEFT JOIN RSTCATALOG    ON SPAAPPTADDONS.ITEMNO  = RSTCATALOG.C_ITEM

另外,我建议您使用表别名来使查询更短,更易读。像这样:

FROM SPAAPPOINTMENTS    apt
LEFT JOIN SPAAPPTADDONS add ON apt.APTID  = add.APTID 
LEFT JOIN RSTCATALOG    rst ON add.ITEMNO = rst.C_ITEM