我试图在MySQL中使用聚合函数来返回帐户的余额。但是我一直在犯错误,这让我疯了!
SELECT cast(sum((number_sold - number_bought) * price) as unsigned) as 'Balance'
FROM transactions
返回:
#2014 - Commands out of sync; you can't run this command now
这是一个SQLfiddle:http://sqlfiddle.com/#!2/26297/1
任何帮助表示赞赏!
答案 0 :(得分:1)
正如Xepoch所提到的here,
如果是减法的左侧或右侧 运算符是无符号的,结果也是无符号的。你可以改变 这可以通过设置NO_UNSIGNED_SUBTRACTION SQL模式。
或者,您也可以明确地将无符号值转换为 签署bigint值,然后进行减法。
因此,
SELECT (cast(number_sold AS signed) - cast(number_bought AS signed)) * price AS 'Balance'
FROM transactions