Home > Database > Mysql Tutorial > body text

mysql-求助,联合查询多次求和同一张表同一字段,求和结果总是不对

WBOY
Release: 2016-06-06 09:45:16
Original
1477 people have browsed it

mysqlphp

SELECT so.*,t1.时间 AS 最后记账时间,IFNULL(SUM(t1.金额),0) AS 总金额,IFNULL(SUM(金额),0) AS 定单金额,IFNULL(SUM(t3.金额),0) AS 销售单金额'
.' FROM 订单表 AS so LEFT JOIN '
.'(SELECT ID,金额,编号,时间 FROM 流水表 WHERE 类型 = 1 order by datatime desc)t1 ON ((t1.编号 = so.订单号) OR (t1.编号 = so.定单号)) LEFT JOIN '
//.'(SELECT ID,金额,编号 FROM 流水表 WHERE 类型 = 1)t2 ON t2.编号 = so.定单号 LEFT JOIN '
.'流水表 AS t2 ON t2.编号 = so.定单号 AND t2.类型 = 1 LEFT JOIN '
.'(SELECT 金额,编号 FROM 流水表 WHERE 类型 = 1 AND 科目 = "销售单定金")t3 ON t3.编号 = so.订单

我要根据订单里面的订单号或者定单号,多次求和得到订单总金额、定单金额及销售单金额,所有金额都记录在流水表的金额中,通过类型及编号来区分,我试了很久,销售金额或者定定定单金额求和总是会有误差,总是会多出二三倍,我估计应该是三个查询会把把的有查询结果汇总。
求高手指点这个联合查询应该怎么写。
为了方便大家读懂,我已经把表名及字段名等都换成了中文。

等不到回答,自己已经搞定了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!