我正在执行此查询。
select (select FollowersCount from webanalysis.DataTwUser_followers_history where DataTwUserID='80943' and Date <='2015-01-12' order by Date desc limit 1) - (select FollowersCount from webanalysis.DataTwUser_followers_history where DataTwUserID='80943' and Date >='2014-12-12' order by Date limit 1) as new;
它给了我“18446744073709551606”作为答案,但我想要“-9”作为答案。
如果我执行这个查询1,
select FollowersCount from webanalysis.DataTwUser_followers_history where DataTwUserID='80943' and Date <='2015-01-12' order by Date desc limit 1;
它会给我“193”作为答案。如果执行此查询2
select FollowersCount from webanalysis.DataTwUser_followers_history where DataTwUserID='80943' and Date >='2014-12-12' order by Date limit 1;
它会给我“202”作为答案,所以我想这样做,
193 - 202 = -9。
我哪里错了?需要帮助。
最佳答案
你有无符号整数值,你正在做减法,MySQL将结果限制为无符号整数......该值溢出(下溢)下限,并“换行”。
要解决此问题,请在减法之前将无符号值转换或转换为有符号值。
SELECT CAST(( val1 ) AS SIGNED) - CAST(( val2 ) AS SIGNED) AS foo
(用返回无符号整数值的子查询替换 val1
和 val2
)
如果您愿意,可以使用 CONVERT 函数代替 CAST
SELECT CONVERT(( val1 ),SIGNED) - CONVERT(( val2 ),SIGNED) AS foo
关于mysql 没有给出想要的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27895873/