最长的时间让我头疼。我有两个表,一个表由客户摘要组成,另一个表是他们购买的库存(所购买的库存每个在其自己的行上)。我想返回具有一定数量和满足特定条件的订单的客户信息。我以为自己有,但猜不到。
到目前为止,这是我所拥有的:
SELECT a.customerTableStuff, SUM(b.quantity)
FROM order_sum a, order_inv b
WHERE a.ordernum = b.ordernum AND status = 2
HAVING quantity <= 2 OR (quantity = 2 AND b.Code NOT LIKE 'C1%')
ORDER BY a.ordernum DESC
该总和似乎有效,但是,问题代码不一样。如果行中的另一个产品代码不同,则表示选择为真...
我也尝试过Join并获得相同的结果。
提前致谢。
最佳答案
让您应该尝试UNION
SELECT a.customerTableStuff, SUM(b.quantity)
FROM order_sum a, order_inv b
WHERE a.ordernum = b.ordernum AND status = 2
HAVING quantity <= 2
ORDER BY a.ordernum DESC
UNION
SELECT a.customerTableStuff, SUM(b.quantity)
FROM order_sum a, order_inv b
WHERE a.ordernum = b.ordernum AND status = 2
AND b.Code NOT LIKE 'C1%'
HAVING quantity = 2
ORDER BY a.ordernum DESC
关于mysql - mySQL从联接表中排除行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34364089/