如何通过自定义规则进行订购,例如如何订购4,2,1,3 [英] How to order by custom rule, e.g. how to order like 4,2,1,3

查看:60
本文介绍了如何通过自定义规则进行订购,例如如何订购4,2,1,3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定标题是否正确,很可能不是:)

I am not sure that the title is saying it right, most probably it does not :)

所以我有products桌子,我想按制造季节来订购它们.

So I have products table and I want to order them by the season they are made for.

如果'spring'是1,'summer'-2,'autumn'-3和'winter'-4,我该如何订购它们,以便先显示"summer",然后显示"spring",然后是冬季",最后是秋季".就像2,1、4、3一样.

If 'spring' is 1, 'summer' - 2 , 'autumn' - 3 and 'winter' - 4, how can I order them in such way so it shows the 'summer' first, then 'spring', then 'winter' and at the end 'autumn'. So like 2,1,4,3.

为了进一步说明,我希望能够更改规则.可能是2,1,4,3或4,2,1,3,依此类推.这是为了显示季节产品在顶部.

And to clarify it more, I want to be able to change the rule. It might be 2,1,4,3 or 4,2,1,3, etc. This is to show the season products on top.

希望这种解释有助于解决问题.欢迎任何解决方案.数据库是MySQL,语言是PHP.

Hope that explanation helps to get the problem. Any solutions are welcomed. Database is MySQL, language PHP.

推荐答案

创建一个季节" ID字符串.然后按字段顺序进行排序:

Create a string of "season" ids. Then do the order by field thing:

SELECT * FROM foo ORDER BY FIELD(season_id, 4, 2, 1, 3);

用内插的php变量替换4,2,1,3部分.

Replace the 4, 2, 1, 3 part with interpolated php variable.

这篇关于如何通过自定义规则进行订购,例如如何订购4,2,1,3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆