0

我有一个相当大的查询,它的快递部分使它不起作用我不知道为什么,没有出现错误,但也许我没有看到任何东西......

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 的任何引用一起删除,它就可以正常工作。否则不会。

关于可能导致这种情况的任何想法?

我基本上是在创建一个搜索查询。检查数据库中的许多表。如果您对我如何使它变得更好有任何建议,那就太好了:)

4

1 回答 1

1

我猜你需要一个 LEFT OUTER JOIN 在快递员 cr 上。

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。

有关更多信息,请参阅关于 JOIN 的图示。

于 2013-03-31T12:31:02.823 回答