MYSQL - JOIN表过滤器不起作用

时间:2016-05-03 12:28:58

标签: mysql join


我有四个表,每个表连接一些id,请看下面的查询我是如何得到的

SELECT 
tax_rates.name, 
IF( products.tax_method = 0, 'Inclusive', 'Exclusive') AS type, 
IF((SUM(purchases.grand_total) > 0),(SUM(purchases.grand_total)),(SUM(purchase_items .subtotal))) AS total_amount, 
IF((SUM(purchases.order_tax) > 0),(SUM(purchases.order_tax)),SUM(purchase_items .item_tax)) AS tax_amount 
FROM tax_rates 
LEFT JOIN purchases ON purchases.order_tax_id = tax_rates.id 
LEFT JOIN purchase_items ON purchase_items.tax_rate_id = tax_rates .id 
LEFT JOIN products ON products.id = purchase_items.product_id 
WHERE purchases.warehouse_id = 1 
GROUP BY tax_rates.id, products.tax_method 
ORDER BY tax_rates.name desc LIMIT 0, 10

上面的查询没有返回任何结果,但是如果我删除了WHERE purchases.warehouse_id = 1,那么它会显示结果。我不知道我在哪里做错了。请你纠正我。

很抱歉告诉我这件事,我试图获得采购订单税并购买物品税 特别是商店和日期

输出

name       type     total_amount    tax_amount 

VAT @20%   Inclusive    11005.2000  1834.2000
VAT @10%   Inclusive    165.0000    15.0000
No Tax     Exclusive    204771.4000     0.0000
GST @6%    Exclusive    7155.0000   405.0000
GST @6%    Inclusive    7155.0000   405.0000

谢谢

2 个答案:

答案 0 :(得分:0)

数据类型? purchases.warehouse_id ='1'?

答案 1 :(得分:0)

当您使用左连接表的字段时,不应在where子句中添加条件。所以删除where子句并使用如下

LEFT JOIN purchases ON purchases.order_tax_id = tax_rates.id and purchases.warehouse_id = 1

如果您尝试在where子句中添加左连接表字段,它将变为INNER JOIN。因此,根据您的数据,它可能不会显示任何行。