简体   繁体   English

MySQL两列总和错误结果

[英]MySQL sum of two columns wrong result

My SQL query which contains a sum of two values from two different tables, comes out with a wrong result for some of the queries. 我的SQL查询包含来自两个不同表的两个值的总和,但对于某些查询却给出了错误的结果。

Table looking like this: 表格看起来像这样:


table 1 表格1

    ID  |  name  |  value         
    1   |  bla   |  88.666666     
    2   |  hi    |  90.555555      
    3   |  bye   |  80.444444

table 2 表2

    ID  |  name  |  value         
    1   |  h     |  1.007275     
    2   |  na    |  22.005555      
    3   |  nh    |  23.007878

Then I want to add the values of bla and h together. 然后,我想将bla和h的值加在一起。 I then do a simple query 然后我做一个简单的查询

SELECT a.`value`+b.`value` AS totalvalue from `table 1` a CROSS JOIN table 2 WHERE a.`ID` = 1 AND b.`ID` = 1 

The result should be something like: 结果应该是这样的:

    89.673941

But it reality i get: 但现实我得到:

89.6739400000000000000000001

Both table types are TEXT, i have tried with DOUBLE but no difference. 两种表类型均为TEXT,我尝试使用DOUBLE,但没有区别。 The tables constructed is just an small example, the tables i use is larger, and approx. 构造的表只是一个小例子,我使用的表更大,大约为2。 1 out of 3 results may be wrong as the example shows. 如示例所示,3个结果中有1个可能是错误的。 I hope you can help. 希望您能提供帮助。

Try with CAST as DECIMAL like 像DECIMAL一样尝试CAST

SELECT cast((a.`value`+b.`value`) AS decimal(10,6)) AS totalvalue
FROM `table 1` a
CROSS JOIN TABLE 2
WHERE a.`ID` = 1
  AND b.`ID` = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM