我有3张桌子但是现在我只用2来测试
AccntTbl
id accnt amount date
---------------------------------------
1 xxx 10.00 1/1/2016
2 yyy 20.00 1/1/2016
3 zzz 30.00 1/1/2016
IssuedTbl
id accnt issued dateIssue
------------------------------------------
1 xxx 5.00 1/10/2016
1 xxx 5.00 2/14/2016
2 yyy 10.00 2/15/2016
我希望结果看起来像这个......在JANUARY中
id accnt amount issued balance
-------------------------------------------------
1 xxx 10.00 5.00 5.00
2 yyy 20.00 0.00 20.00
3 zzz 30.00 0.00 30.00
如果它在FEBRUARY
id accnt amount issued balance
-------------------------------------------------
1 xxx 10.00 10.00 0.00
2 yyy 20.00 10.00 10.00
3 zzz 30.00 0.00 30.00
这是我提出的查询
SELECT AccntTbl.id, AccntTbl.accnt, AccntTbl.amount,
SUM(IssuedTbl.issued) as issued
FROM AccntTbl LEFT JOIN IssuedTbl
ON AccntTbl.id = IssuedTbl.id
WHERE (MONTH(IssuedTbl.dateIssue BETWEEN 1 AND 2)) AND
(YEAR(AccntTbl.date)= 2016)
GROUP BY AccntTbl.id, AccntTbl.accnt, AccntTbl.amount
但结果不是我所期望的只有来自1 & 2
的id会从结果中显示...我想要显示来自AccntTbl的所有值以及来自IssuedTbl..glad的值从任何帮助..tnx:)
答案 0 :(得分:1)
当您使用LEFT OUTER JOIN
并且想要从 Right 表中过滤某些记录时,您需要将右表过滤器移至{{1}条件else ON
将隐式转换为LEFT OUTER JOIN
为了简短起见,INNER JOIN
条款拒绝 WHERE
行,NULL
为非匹配记录生成
LEFT OUTER JOIN