mysql两表相减,如何显示出所有结果

PQianbei 2014-04-15 11:05:52
有两表,hctotaltmp(总表),hctotalout(出库表),字段分别为catalogue,hc,number。现已实现php前台显示出剩余的各数量。但是如何同时显示在总表中,但未出库的数量。
$sql = "SELECT hctotaltmp.catalogue,hctotaltmp.hc, hctotaltmp.number - ifnull(hctotalout.number, 0)
FROM
hctotalout
LEFT JOIN hctotaltmp ON hctotalout.hc = hctotaltmp.hc
GROUP BY
hc DESC
ORDER BY
catalogue DESC ";

这样的话,只能存在于hctotalout表里面的数据才会显示出来。我想把所有的都显示出来,没出库的数量就显示为总表中的数量,出库的话就是总表数量减出库表对应字段的数量。
...全文
567 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
benluobo 2014-04-15
  • 打赏
  • 举报
回复
改成 hctotaltmp LEFT JOIN hctotalout
PQianbei 2014-04-15
  • 打赏
  • 举报
回复
引用 2 楼 benluobobo 的回复:
LEFT JOIN 左右换一下
神马意思?
benluobo 2014-04-15
  • 打赏
  • 举报
回复
LEFT JOIN 左右换一下
WWWWA 2014-04-15
  • 打赏
  • 举报
回复
select catalogue,sum(number) from ( select * from hctotaltmp union all select catalogue,hc,-1*number from hctotalout) a group by catalogue
PQianbei 2014-04-15
  • 打赏
  • 举报
回复
引用 1 楼 WWWWA 的回复:
select catalogue,sum(number) from ( select * from hctotaltmp union all select catalogue,hc,-1*number from hctotalout) a group by catalogue
相当感谢,已可用!

56,687

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧