有谁能告诉我为什么会发生以下情况,以及如何解决?
我有一个带有ORDERBY子句的MySQL查询,如下所示..

ORDER BY (did_voteup-did_votedown) DESC, did_voteup DESC

所以它应该根据他们的“有效”评级来排序结果,如…
1st. 10 up - 1 down = 9 effective
2nd. 10 up - 5 down = 5 effective
3rd. 10 up - 7 down = 3 effective

然而,正如您在my page here上看到的,它的命令是错误的,并且给了我这个..
1st. 1 up - 3 down = -2 effective
2nd. 16 up - 6 down = 10 effective
3rd. 15 up - 5 down = 10 effective

显然,第一排不应该在那里。
更多信息..
CREATE TABLE dictionary_definitions (
  did_id int(11) unsigned NOT NULL auto_increment,
  did_wordid int(11) unsigned NOT NULL default '0',
  did_userid int(11) unsigned NOT NULL default '0',
  did_status tinyint(1) unsigned NOT NULL default '0',
  did_date int(11) NOT NULL default '0',
  did_definition text,
  did_example text,
  did_votecheck int(11) NOT NULL default '0',
  did_voteup smallint(5) unsigned NOT NULL default '0',
  did_votedown smallint(5) unsigned NOT NULL default '0',
  PRIMARY KEY (did_id),
  KEY (did_wordid),
  KEY (did_userid)
) ENGINE=MyISAM;

SELECT a.did_id, a.did_userid, a.did_definition, a.did_example,
    a.did_votecheck, a.did_voteup, a.did_votedown, b.user_name, b.user_extra8
FROM dictionary_definitions AS a LEFT JOIN users AS b ON a.did_userid=b.user_id
WHERE did_wordid=4
ORDER BY (did_voteup-did_votedown) DESC, did_voteup DESC LIMIT 0, 5

最佳答案

这是一个已知的issue regarding subtraction from unsigned integers
整数值之间的减法,其中一个是无符号类型,产生无符号
默认结果。如果结果本来是否定的,那么
最大整数值。如果启用了NO_UNSIGNED_SUBTRACTIONsql模式,结果是
没有。
参考:Numeric Types

关于sql - MySQL查询疯了吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1517556/

10-08 22:05