mysql查询问题 - 生成重新排序报告

时间:2011-07-31 09:07:21

标签: mysql

我正在处理以下查询: -

SELECT group,item,buffer,stock,(stock-buffer) AS shortexcess
FROM stkmain, stkbalance, stkbuffer
WHERE stkmain.item = stkbalance.item
AND stkmain.item = stkbuffer.item

这个查询工作正常。我想添加另一个来自'sale'表的销售额。修订后的查询是: -

SELECT group,item,buffer,stock,(stock-buffer) AS shortexcess, quantity
FROM stkmain, stkbalance, stkbuffer, sale
WHERE stkmain.item = stkbalance.item
AND stkmain.item = stkbuffer.item
AND stkmain.item = sale.item

此查询的问题是它仅显示出售表中出现的那些行,而不显示stkmain表的所有行。 请告诉我如何纠正这个问题。

2 个答案:

答案 0 :(得分:4)

在sktmain上使用Left Join获取所有sktmain行。

SELECT group,item,buffer,stock,(stock-buffer) AS shortexcess, quantity
FROM stkmain
LEFT OUTER JOIN stkbalance ON stkmain.item = stkbalance.item
...

答案 1 :(得分:1)

SELECT `group`, item,buffer, stock, stock-buffer AS shortexcess 
FROM stkmain
JOIN stkbalance ON skmain.item = stkbalance.item
JOIN stkbuffer ON stkmain.item = stkbuffer.item
OUTER JOIN sale ON stkmain.item = sale.item

组是reserved keyword,应该通过反引号进行转义。