MySQL在减去无符号值时如何使负结果成为可能?

时间:2012-11-08 11:22:01

标签: mysql

我有三个表由左连接加入。这是代码:

SELECT
(LEAST(`a`.`price, `b`.`price`) - `c`.`price`) AS `diff`
...
ORDER BY `diff` DESC

问题:cprice大于最小值,因此减法为负并且抛出BIGINT UNSIGNED值超出范围。 我怎么能不把这个荒谬的错误抛到脑后呢? 这是结果数据,我没有修改表中的实际数据,为什么它不允许我这样做呢? 我已经尝试了CAST(LEAST(...) AS SIGNED)并在LEAST中将这两列列为已签名,但都没有效果。

2 个答案:

答案 0 :(得分:7)

在LEAST之前和在减去

之前,将每个数字转换为SIGNED
SELECT
(LEAST(CAST(`a`.`price` AS SIGNED), CAST(`b`.`price` AS SIGNED)) - CAST(`c`.`price` AS SIGNED)) AS `diff`
...
ORDER BY `diff` DESC

答案 1 :(得分:1)