程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Select from 1 table sum from 2 but 1 table has an AND condition大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决SELEct from 1 table sum from 2 but 1 table has an AND condition?

开发过程中遇到SELEct from 1 table sum from 2 but 1 table has an AND condition的问题如何解决?下面主要结合日常开发的经验,给出你关于SELEct from 1 table sum from 2 but 1 table has an AND condition的解决方法建议,希望对你解决SELEct from 1 table sum from 2 but 1 table has an AND condition有所启发或帮助;

感谢您对 2 个表中 SUM 的上一个查询 (SQL Query that SELEcts a @R_450_8620@n in table 1 and uses that to SELEct sum in table 2) 的帮助,我现在有一个表的附加条件。我想添加 WHERE Group1 = 1 AND IN/OUT = 'OUT'

我有 3 张桌子,

  1. 名称、群组
  2. 姓名、付款
  3. 姓名、付款和输入/输出

我只想对表 3 中的 OUT Payments 求和,我只得到总付款 所以 FAR 是:

SELECT t1.name1,SUM(t2.SALE2),SUM(t3.SALE3)
FROM table1 t1 JOIN table2 t2 ON t1.name1 = T2.NAME2
JOIN table3 t3 ON t1.name1 = t3.name3
WHERE group1 = 1
GROUP BY t1.name1

如果没有要求和的数据,我还想添加一个零,而不是删除整个记录,目前,如果某个名称在表 3 中没有付款但在表 2 中有付款,则会删除该记录。

解决方法

请检查下面的查询=>
按名称获取 OutPayment 组

SELECT  t1.names,SUM(t3.Payments) As OutPayment 
FROM TABLE3 as t3
INNER JOIN TABLE1 as t1 ON t1.names = t3.Names
INNER JOIN table2 as t2 ON t1.names = T2.NAMEs
WHERE t1.GroupID = 1 AND t3.INOROUT=2 --INOROUT =2 is OUT and 1 is IN
GROUP BY t1.names;


获取 @R_512_10586@lOutPayment

SELECT  SUM(t3.Payments) As @R_512_10586@lOutPayment 
FROM TABLE3 as t3
INNER JOIN TABLE1 as t1 ON t1.names = t3.Names
INNER JOIN table2 as t2 ON t1.names = T2.NAMEs
WHERE t1.GroupID = 1 AND t3.INOROUT=2; --INOROUT =2 is OUT and 1 is IN

注意:代码也在 DBFiddle check the Demo Query Link

大佬总结

以上是大佬教程为你收集整理的Select from 1 table sum from 2 but 1 table has an AND condition全部内容,希望文章能够帮你解决Select from 1 table sum from 2 but 1 table has an AND condition所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。