简体   繁体   English

如何按自定义规则订购,例如如何订购4,2,1,3

[英]How to order by custom rule, e.g. how to order like 4,2,1,3

I am not sure that the title is saying it right, most probably it does not :) 我不确定标题是否说得对,很可能不是:)

So I have products table and I want to order them by the season they are made for. 所以我有products表,我想在他们制作的季节订购。

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'. 如果'spring'是1,'summer' - 2,'autumn' - 3和'winter' - 4,我怎样才能以这样的方式订购它们以便首先显示'summer',然后是'spring',然后是'winter' '并在'秋天'结束。 So like 2,1,4,3. 就像2,1,4,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. 可能是2,1,4,3或4,2,1,3等。这是为了展示季节产品。

Hope that explanation helps to get the problem. 希望解释有助于解决问题。 Any solutions are welcomed. 欢迎任何解决方案。 Database is MySQL, language PHP. 数据库是MySQL,语言PHP。

This should work for all major dbms: 这适用于所有主要的dbms:

order 
   by case when season = 2 then 1 
           when season = 1 then 2
           when season = 4 then 3
           when season = 3 then 4 
       end;

You could also add a column to your season table to indicate how they should be sorted: 您还可以在季节表中添加一列,以指示它们应如何排序:

table season(
   season_id int
  ,ordinal   int 
);


select ...
  from tab t
  join seasons s on(t.season_id = s.season_id)
 order 
    by s.ordinal;

...or, if you feel like writing non-portable code you can use: ...或者,如果您想编写非可移植代码,您可以使用:

order by field(season_id, 2,1,4,3);

Create a string of "season" ids. 创建一串“季节”ID。 Then do the order by field thing: 然后按字段顺序执行:

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

Replace the 4, 2, 1, 3 part with interpolated php variable. 用插值的php变量替换4,2,1,3部分。

暂无
暂无

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

相关问题 如何在查询中将数量从一个表分配到另一个表? 例如,拣配的订单项目数量与实际订单项目数量 - How to allocate quantities from one table to another in a query? e.g. picked order item quantities against actual order item quantities SQL:将例如1,2,4,5,7修正为1,2,3,4,5(队列顺序) - SQL: Correct e.g. 1,2,4,5,7 to 1,2,3,4,5 (queue order) Oracle - 有序列 ID 例如 ORDER BY 1 被允许在哪里? - Oracle - where the orderly column ID e.g. ORDER BY 1 is allwed? SQL与UNION一起使用ORDER BY不能正确地对数字排序(例如8之前的10) - SQL Using ORDER BY with UNION doesn't sort numbers correctly (e.g. 10 before 8) 如何使用SQL函数(例如,NOW())更新updateble ResultSet - How to update updateble ResultSet with SQL Function e.g. NOW() 在PL / SQL中使用带有“LIKE%”的变量(例如“变量%”)? - Use a variable with “LIKE %” (e.g. “variable%”) in PL/SQL? 如何找出MySQL字符集排序规则中各种符号的相对顺序(例如utf8_unicode_ci)? - How can I find out the relative order of various symbols in a MySQL character set collation (e..g utf8_unicode_ci)? 如何创建存储过程以在 Sql server 中创建表,其中表的名称将按年份更改,例如 Order2019 - How to make a store procedure to create table in Sql server in which name of table will be changed by year e.g Order2019 如何在mysql中通过自定义属性进行排序 - How to order by custom property in mysql 如何设置一个输出参数,它是两个字段的组合,例如fullname = firstname + lastname? - How to set an output parameter that is the combination of two fields, e.g., fullname=firstname+lastname?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM