小编典典

MySQL查询变得疯狂了吗?

sql

谁能告诉我以下原因的原因以及如何解决?

我有一个带有ORDER BY子句的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

但是,正如您在此处的我的页面上所看到的那样,这是在错误地订购它们,然后给我这个。

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

阅读 148

收藏
2021-03-23

共1个答案

小编典典

关于从无符号整数中减去是一个已知的问题

默认情况下,整数值之间的减法(其中一个的类型为UNSIGNED)将产生无符号的结果。如果结果否则为负,则它将成为最大整数值。如果NO_UNSIGNED_SUBTRACTION启用了SQL模式,则结果为负。

参考:数值类型

2021-03-23