Sybase = *加入mysql右外连接

时间:2014-01-09 15:19:20

标签: mysql sybase outer-join

我有以下Sybase sql语句,它包含使用syntax = *的右外连接(我认为)。我试图在mysql中复制它并且失败可怕。任何人都可以建议我如何在from部分中说明正确的外连接。

SELECT database1.inst.regionAbb,
       database1.inst.regionalCode,
       database1.inst.instCode,
       database1.inst.name,
       database1.inst.yCode,
       database1.blac.customerCode,
       database1.inst.transportCode,
       database1.inst.Agreement,
       database1.addr.telephone,
       database1.addr.fax,
       database1.addr.email,
       database1.addr.id contactaddr,
       database1.inst.nameAbb,
       database1.inst.messageType,
       database1.inst.closedFrom,
       database1.inst.closedTo,
       database1.inst.costScheme1,
       database1.inst.costScheme2,
       database1.inst.costScheme3,
       database1.inst.costScheme4,
       database1.inst.costScheme5,
       now() updated

FROM database1.Table1 inst,
     database1.Table2 addr,
     database1.Table3 blac

WHERE ((database1.addr.instCode = database1.inst.instCode) **
       AND ((database1.blac.instCode =* database1.inst.instCode)**
       AND (database1.blac.preferred = "Y")))
       AND ((database1.addr.id = database1.inst.Address
       AND database1.inst.lendingTable2 != 0)
       OR (database1.addr.id = database1.inst.mainTable2
       AND database1.inst.lendingTable2 = 0))

2 个答案:

答案 0 :(得分:0)

我不熟悉Sybase,但我认为这就是你想要的:

SELECT * FROM 
FROM database1.Table1 inst,
INNER JOIN database1.Table2 addr ON addr.instCode = inst.instCode
LEFT JOIN database1.Table3 blac ON blac.instCode = inst.instCode
WHERE blac.preferred = "Y"
  OR (blac.preferred IS NULL AND
       ((addr.id = inst.Address    AND inst.lendingTable2 != 0)
     OR (addr.id = inst.mainTable2 AND inst.lendingTable2 = 0)))

这假设它只会在没有blac记录的情况下进行最终检查 - 如果您确实想要检查是否没有blac记录或blac.preferred != 'Y'那么您可以删除blac.preferred IS NULL AND以及额外的括号对。

答案 1 :(得分:0)

SELECT * FROM ((database1.Table1 inst left join database1.Table3 blac on inst.instCode = blac.instCode and blac.preferred =“Y”) join database1.Table2 addr on(addr.instCode = inst.instCode and((addr.id = inst.lendingTable2 and inst.lendingTable2<> 0) 或((addr.id = inst.mainTable2)和(inst.lendingTable2 = 0)))));