简体   繁体   English

MySQL:如何使用ALTER TABLE基于两个值的差排序表

[英]MySQL: How can I order a table based upon the difference of two values using ALTER TABLE

I want to sort a MySQL table using ALTER TABLE (so it stays sorted), and it works fine when I simply put in 1 column name like this: 我想使用ALTER TABLE对MySQL表进行排序(因此它保持排序),当我简单地这样输入1列名称时,它就可以正常工作:

ALTER TABLE data ORDER BY upvotes DESC; 

But I want to sort it based upon the difference of two values, and when I do that I get "Unrecognized alter operation." 但是我想根据两个值的差对它进行排序,然后我得到“无法识别的变更操作”。

This is the code I want to work: 这是我要工作的代码:

ALTER TABLE data ORDER BY (upvotes-downvotes) DESC; 

Is there a way to do this, or am I just getting the syntax wrong? 有没有办法做到这一点,还是我只是弄错了语法? Thanks in advance! 提前致谢!

As mysql documentation on alter table says (highlighting is mine): 正如mysql在alter table上的文档所说(突出显示是我的):

ORDER BY syntax permits one or more column names to be specified for sorting, each of which optionally can be followed by ASC or DESC to indicate ascending or descending sort order, respectively. ORDER BY语法允许指定一个或多个列名进行排序,可以选择在每个列名后跟随ASC或DESC分别指示升序或降序。 The default is ascending order. 默认为升序。 Only column names are permitted as sort criteria; 只允许使用列名作为排序条件; arbitrary expressions are not permitted. 不允许使用任意表达式。 This clause should be given last after any other clauses. 该条款应在其他条款之后最后给出。

As @juergend suggested in his comment, the possible workaround is to store the results of the calculation in a field and use that for sorting. 正如@juergend在其评论中建议的那样,可能的解决方法是将计算结果存储在字段中,然后将其用于排序。

Also, I'm not sure why you are still stuck with myisam table type. 另外,我不确定为什么您仍然对myisam表类型感到困惑。 I would definitely consider switching over to innodb. 我肯定会考虑切换到innodb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM