我有一个相当大的查询,它的快递部分是让它不起作用我不确定为什么,没有错误出现,但也许我没有看到的东西......
SELECT o.purchaseNo, o.dateCreated, c.Name, m.mouldName, cr.courierName
FROM products AS p
INNER JOIN orderedProducts AS op ON op.productID = p.productID
INNER JOIN orders AS o ON op.orderID = o.orderID
INNER JOIN Couriers AS cr ON cr.couriersID = o.couriersID
INNER JOIN customers AS c ON c.customerID = o.customerID
INNER JOIN Moulds AS m ON m.mouldID = p.mouldID
WHERE c.Name LIKE '%john%'
OR p.name LIKE '%john%'
OR c.Name LIKE '%john%'
OR c.POC1 LIKE '%john%'
OR c.POC2 LIKE '%john%'
OR c.Address1 LIKE '%john%'
OR c.Address2 LIKE '%john%'
OR c.Suburb LIKE '%john%'
OR c.State LIKE '%john%'
OR c.Phone LIKE '%john%'
OR c.Email LIKE '%john%'
OR c.ABN LIKE '%john%'
OR c.Fax LIKE '%john%'
OR c.CompanyName LIKE '%john%'
OR o.purchaseNo LIKE '%john%'
OR o.dateCreated LIKE '%john%'
OR cr.courierName like '%john%'
Couriers cr是导致问题的因素。如果我将它与对cr的任何引用一起删除它就可以了。否则就不会。
关于可能导致这种情况的任何想法?
我基本上是在创建搜索查询。检查数据库中的许多表。如果你对我如何做得更好有任何建议,那就太好了:))
答案 0 :(得分:1)
我猜你需要在快递员处进行LEFT OUTER JOIN。
SELECT o.purchaseNo, o.dateCreated, c.Name, m.mouldName, cr.courierName
FROM products AS p
INNER JOIN orderedProducts AS op ON op.productID = p.productID
INNER JOIN orders AS o ON op.orderID = o.orderID
INNER JOIN customers AS c ON c.customerID = o.customerID
INNER JOIN Moulds AS m ON m.mouldID = p.mouldID
LEFT OUTER JOIN Couriers AS cr ON cr.couriersID = o.couriersID
WHERE c.Name LIKE '%john%'
OR p.name LIKE '%john%'
OR c.Name LIKE '%john%'
OR c.POC1 LIKE '%john%'
OR c.POC2 LIKE '%john%'
OR c.Address1 LIKE '%john%'
OR c.Address2 LIKE '%john%'
OR c.Suburb LIKE '%john%'
OR c.State LIKE '%john%'
OR c.Phone LIKE '%john%'
OR c.Email LIKE '%john%'
OR c.ABN LIKE '%john%'
OR c.Fax LIKE '%john%'
OR c.CompanyName LIKE '%john%'
OR o.purchaseNo LIKE '%john%'
OR o.dateCreated LIKE '%john%'
OR cr.courierName like '%john%'
我猜测背后的原因:
如果删除Couriers,则会显示结果集。所以你没有找到cr.couriersID = o.couriersID
上的匹配,仍然如你想要的结果集尽管匹配或不匹配你需要LEFT OUTER JOIN。