简体   繁体   中英

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.

Table looking like this:


table 1

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

table 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. 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. The tables constructed is just an small example, the tables i use is larger, and approx. 1 out of 3 results may be wrong as the example shows. I hope you can help.

Try with CAST as DECIMAL like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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