MySQL, how to make negative results possible when subtracting unsigned values?

I have three tables joined by a left join. Here is the code:

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

Problem: c . price greater than LEAST, so the subtraction is negative and throws the BIGINT UNSIGNED value out of range. How can I make NOT throw this ridiculous mistake?
This is the result data, I do not change the actual data in the table, so why does this not allow me to do this normally? I tried CAST(LEAST(...) AS SIGNED) and discarded both columns inside LEAST as signed, did not work.

+4
source share
2 answers

SIGNAL each number before LEAST and before subtraction

 SELECT (LEAST(CAST(`a`.`price` AS SIGNED), CAST(`b`.`price` AS SIGNED)) - CAST(`c`.`price` AS SIGNED)) AS `diff` ... ORDER BY `diff` DESC 
+5
source

Source: https://habr.com/ru/post/1444779/


All Articles