mysql总和计算不正确

mysql总和计算不正确,mysql,Mysql,我对MySQL有怀疑。我有三张桌子: 新订单表: +------------+---------+----------+------------+---------------+-----------+----------+-----------+----------+------+----------+------------+-----------+ | neworderid | orderno | customer | date | stocklocation | produc

我对MySQL有怀疑。我有三张桌子:

新订单表:

+------------+---------+----------+------------+---------------+-----------+----------+-----------+----------+------+----------+------------+-----------+
| neworderid | orderno | customer | date       | stocklocation | product   | quantity | unitprice | discount | tax  | total    | grandtotal | status    |
+------------+---------+----------+------------+---------------+-----------+----------+-----------+----------+------+----------+------------+-----------+
| ORD001     | ORD-001 | abc      | 2018-02-27 | Bangalore     | sssssssss |        2 |     28000 |        2 |   18 |  64758.4 |          0 | Approved  |
| ORD002     | ORD-002 | abc      | 2018-03-05 | Bangalore     | sssssssss |        8 |     28000 |        2 |   18 | 259033.6 |          0 | Approved  |
| ORD003     | ORD-003 | hhh      | 2018-03-05 | Haveri        | sssssssss |        2 |     28000 |       50 |   30 |    36400 |          0 | Cancelled |
+------------+---------+----------+------------+---------------+-----------+----------+-----------+----------+------+----------+------------+-----------+  
addproductfromsupplier:

+-------------------+-----------+------------+--------------+---------------+---------------------------------------------------------------------------+-------------+----------+-----------+-------+-------------+------------+-------------+
| supplierproductid | invoiceno | date       | suppliername | stocklocation | address                                                                   | productname | quantity | unitprice | tax   | total       | grandtotal | status      |
+-------------------+-----------+------------+--------------+---------------+---------------------------------------------------------------------------+-------------+----------+-----------+-------+-------------+------------+-------------+
| SUPR001           | CM-001    | 2018-02-12 | COMPANY1     | Bangalore     | AAAAAASSSSSSDDDDD                                                         | sssssssss   |       10 |   2500.00 |  5.00 |  26250.0000 |       NULL | received    |
| SUPR002           | CM-001    | 2018-02-12 | COMPANY1     | Bangalore     | AAAAAASSSSSSDDDDD                                                         | sssss       |       10 |   1000.00 |  6.00 |  10600.0000 |       NULL | received    |
| SUPR003           | INC-001   | 2017-05-07 | eeee         | Bangalore     |                  qqqq                                                     | sssssssss   |        5 |  28000.00 | 18.00 | 165200.0000 |       NULL | notreceived |
| SUPR004           | INV-0001  | 2018-03-17 | hh           | Haveri        |                                     AAAAAAA                               | sssssssss   |        2 |  28000.00 | 18.00 |  66080.0000 |       NULL | notreceived |
+-------------------+-----------+------------+--------------+---------------+---------------------------------------------------------------------------+-------------+----------+-----------+-------+-------------+------------+-------------+
费用表:

+-----------------+------------+--------+
| description     | date       | amount |
+-----------------+------------+--------+
| office Cleaning | 2018-03-17 |    500 |
+-----------------+------------+--------+  
我无法正确获取查询的总和:

select sum(A.total) as totalsales
     , sum(B.total) as totalpurchase
     , (sum(A.total)-sum(B.total)) as grossprofit
     , sum(C.amount) as expence
     , ((sum(A.total)-sum(B.total))-sum(C.amount)) as netprofit 
  from newordertable A
     , addproductfromsupplier B
     , expensetable C 
 where A.date between '2018-3-05' and '2018-3-18' 
   and B.date between '2018-3-05' and '2018-3-18' 
   and C.date between '2018-3-05' and '2018-3-18'; 
+------------+---------------+--------------------+---------+--------------------+
| totalsales | totalpurchase | grossprofit        | expence | netprofit          |
+------------+---------------+--------------------+---------+--------------------+
|   295433.6 |   132160.0000 | 163273.59999999998 |    1000 | 162273.59999999998 |
+------------+---------------+--------------------+---------+--------------------+

无意冒犯,但这表明对工作方式和执行顺序存在误解。 如果我们把总和和过滤器从中去掉

select *
  from A
     , B
     , C 
we get
+------------+-----------+------------+-----------+------------+--------+
| dte        | total     | dte        | total     | dte        | amount |
+------------+-----------+------------+-----------+------------+--------+
| 2018-02-27 |  64758.40 | 2018-02-12 |  26250.00 | 2018-03-17 | 500.00 |
| 2018-03-05 | 259033.60 | 2018-02-12 |  26250.00 | 2018-03-17 | 500.00 |
| 2018-03-05 |  36400.00 | 2018-02-12 |  26250.00 | 2018-03-17 | 500.00 |
| 2018-02-27 |  64758.40 | 2018-02-12 |  10600.00 | 2018-03-17 | 500.00 |
| 2018-03-05 | 259033.60 | 2018-02-12 |  10600.00 | 2018-03-17 | 500.00 |
| 2018-03-05 |  36400.00 | 2018-02-12 |  10600.00 | 2018-03-17 | 500.00 |
| 2018-02-27 |  64758.40 | 2017-05-07 | 165200.00 | 2018-03-17 | 500.00 |
| 2018-03-05 | 259033.60 | 2017-05-07 | 165200.00 | 2018-03-17 | 500.00 |
| 2018-03-05 |  36400.00 | 2017-05-07 | 165200.00 | 2018-03-17 | 500.00 |
| 2018-02-27 |  64758.40 | 2018-03-17 |  66080.00 | 2018-03-17 | 500.00 |
| 2018-03-05 | 259033.60 | 2018-03-17 |  66080.00 | 2018-03-17 | 500.00 |
| 2018-03-05 |  36400.00 | 2018-03-17 |  66080.00 | 2018-03-17 | 500.00 |
+------------+-----------+------------+-----------+------------+--------+
12 rows in set (0.00 sec)
带过滤器

select *
  from A
     , B
     , C 
 where A.dte between '2018-3-05' and '2018-3-18' 
   and B.dte between '2018-3-05' and '2018-3-18' 
   and C.dte between '2018-3-05' and '2018-3-18'; 
我们得到

+------------+-----------+------------+----------+------------+--------+
| dte        | total     | dte        | total    | dte        | amount |
+------------+-----------+------------+----------+------------+--------+
| 2018-03-05 | 259033.60 | 2018-03-17 | 66080.00 | 2018-03-17 | 500.00 |
| 2018-03-05 |  36400.00 | 2018-03-17 | 66080.00 | 2018-03-17 | 500.00 |
+------------+-----------+------------+----------+------------+--------+
2 rows in set (0.00 sec)
+------------+---------------+-------------+---------+-----------+
| totalsales | totalpurchase | grossprofit | expence | netprofit |
+------------+---------------+-------------+---------+-----------+
|  295433.60 |     132160.00 |   163273.60 | 1000.00 | 162273.60 |
+------------+---------------+-------------+---------+-----------+
1 row in set (0.00 sec)
这是预期的行为,但不是你想要的 把总数加进去

select sum(A.total) as totalsales
     , sum(B.total) as totalpurchase
     , (sum(A.total)-sum(B.total)) as grossprofit
     , sum(C.amount) as expence
     , ((sum(A.total)-sum(B.total))-sum(C.amount)) as netprofit 
  from A
     cross join B
     cross join C 
 where A.dte between '2018-3-05' and '2018-3-18' 
   and B.dte between '2018-3-05' and '2018-3-18' 
   and C.dte between '2018-3-05' and '2018-3-18'; 
我们得到

+------------+-----------+------------+----------+------------+--------+
| dte        | total     | dte        | total    | dte        | amount |
+------------+-----------+------------+----------+------------+--------+
| 2018-03-05 | 259033.60 | 2018-03-17 | 66080.00 | 2018-03-17 | 500.00 |
| 2018-03-05 |  36400.00 | 2018-03-17 | 66080.00 | 2018-03-17 | 500.00 |
+------------+-----------+------------+----------+------------+--------+
2 rows in set (0.00 sec)
+------------+---------------+-------------+---------+-----------+
| totalsales | totalpurchase | grossprofit | expence | netprofit |
+------------+---------------+-------------+---------+-----------+
|  295433.60 |     132160.00 |   163273.60 | 1000.00 | 162273.60 |
+------------+---------------+-------------+---------+-----------+
1 row in set (0.00 sec)
例如,一种方法可能是在子查询中封装总位,然后执行其他位

select Atotal as totalsales
     , Btotal as totalpurchase
     , Atotal-Btotal as grossprofit
     , Camount as expence
     , Atotal - Btotal - camount as netprofit 
from
(  
select (select sum(a.total) from a where A.dte between '2018-3-05' and '2018-3-18' ) atotal,
         (select sum(b.total) from b where b.dte between '2018-3-05' and '2018-3-18' ) btotal,
         (select sum(c.amount) from c where c.dte between '2018-3-05' and '2018-3-18' ) camount
from dual
) s

+------------+---------------+-------------+---------+-----------+
| totalsales | totalpurchase | grossprofit | expence | netprofit |
+------------+---------------+-------------+---------+-----------+
|  295433.60 |      66080.00 |   229353.60 |  500.00 | 228853.60 |
+------------+---------------+-------------+---------+-----------+
1 row in set (0.00 sec)

经过@草莓的编辑,这个问题应该得到一些答案。