无法使用CAST()添加DOUBLE和VARCHAR列

时间:2016-03-29 15:33:30

标签: mysql sql casting int varchar

我的billing表中有两列:

  • cost_per_monthVARCHAR
  • additional_costDOUBLE

当我:

SELECT CONCAT_WS("", "$", FORMAT(db.billing.cost_per_month, 2)) AS "Monthly Cost",
  CONCAT_WS("", "$", FORMAT(db.billing.additional_cost, 2)) AS "Additional Cost",
  CONCAT_WS("", "$", FORMAT(CAST(db.billing.cost_per_month AS DECIMAL(60, 2)) + db.billing.additional_cost, 2)) AS "Total Cost"
  FROM db.billing;

我明白了:

Monthly Cost  Additional Cost  Total Cost
$1,000.00     $                $
$0.00         $400.00          $400.00
$250.00       $250.00          $

而不是:

Monthly Cost  Additional Cost  Total Cost
$1,000.00     $0.00            $1,000.00
$0.00         $400.00          $400.00
$250.00       $250.00          $500.00

我试过了:

  • 使用CAST(db.billing.cost_per_month AS SIGNED)
  • 使用CAST(db.billing.cost_per_month AS UNSIGNED)
  • 删除FORMAT()CONCAT_WS()函数
  • 使用SUM()一堆不同的方式

1 个答案:

答案 0 :(得分:2)

问题是由于行中没有 $file_name = $HTTP_POST_FILES['ufile']['name']; //extension $exx = pathinfo($file_name, PATHINFO_EXTENSION); $random_digit=rand(0000,9999); //generating a unique number $koubiac= strtotime("now"); //current time $dirPath = '../'; //directorypath $new_file_name=$random_digit.".".$exx; //filename is now a random number $uploaddirectory = $dirPath.$random_digit.$koubiac; //upload directory is the path slash random digit concatenated with current time mkdir($random_digit, 0755, true); //making directory $path= $random_digit.'/'.$new_file_name; //path of file to save in MySQL DB table and for copying of file. copy($HTTP_POST_FILES['ufile']['tmp_name'], $path); //copy to path NULL时存在的cost_per_month值。

解决方法是使用additional_cost代替COALESCE()

在你的情况下:

CAST()