我有一个博客。我写了简单的投票系统。我的想法可能是错的。我有一张这样的桌子
id title article TotalVotes VoteSum
当有人投票的文章TotelVotes获得一分如果有人向文章投票,VoteSum cloumn获得1分
当我显示文章的投票时,我这样做
<?php
$Vote = $ROW['TotelVotes']-$ROW['VoteSum'];
?>
它工作正常但在我的头版我想展示热门文章所以我做了这个mysql查询
SELECT TotalVotes-VoteSum AS diff FROM `articles` ORDER BY `diff` DESC
当我这样做时,我收到这样的数字18446744073709551615,例如当TotalVotes小于VoteSum
DATA TYPE IS INT UNSIGNED
我做错了什么?请你帮助我好吗?感谢。
答案 0 :(得分:7)
您可以使用IF
SELECT IF(TotalVotes >= VoteSum, TotalVotes-VoteSum, VoteSum-TotalVotes) AS diff
FROM `articles`
ORDER BY `diff` DESC
<强>更新强>
如果您想获得负值,请将这些列设为INT SIGNED
,以便获得否定值。并直接运行您自己的查询,
SELECT TotalVotes-VoteSum AS diff
FROM `articles`
ORDER BY `diff` DESC
答案 1 :(得分:4)
试试这个:
SELECT cast(TotalVotes as int)-cast(VoteSum as int) AS diff
FROM `articles`
ORDER BY `diff` DESC
如果签名问题在数据库中,这将解决问题。默认情况下,int
数据类型已签名(在MySQL和我知道的每个其他数据库中)。
答案 2 :(得分:1)
使用数学函数ABS
SELECT ABS(TotalVotes-VoteSum) AS diff
FROM `articles`
ORDER BY `diff` DESC