Mysql按用户定义的高级排序

时间:2014-04-24 15:23:29

标签: mysql sorting sql-order-by

道歉,如果这个问题已经得到解答,我已经做了大量的搜索,但没有得到答案。 是否可以通过自己定义的字母排序。

包含单元格name的示例项:

name ASC排序:

Brand1 Model1 Premium
Brand1 Model1 Standard
Brand1 Model1 Normal
Brand1 Model2 Normal
Brand1 Model2 Premium
Brand1 Model2 Standard
Brand1 Model3 Normal
Brand1 Model3 Premium
Brand1 Model3 Standard

但我想要定义自己的订单示例:

Brand1 Model2 Premium <- model2 firt and best version
Brand1 Model2 Normal <- model2 next alphabetical order
Brand1 Model2 Standard <- model2 next alphabetical order
Brand1 Model1 Premium <- model2 next alphabetical by premium first
Brand1 Model1 Normal
Brand1 Model1 Standard
Brand1 Model3 Premium
Brand1 Model3 Normal
Brand1 Model3 Standard

我尝试使用如下:

Case `name`
    when "Brand1 Model2 Premium" then 1
    when "Brand1 Model2 %" then 2
    when "Brand1 Model1 Premium" then 3
    when "Brand1 Model1 %" then 4
    when "Brand1 Model% Premium" then 5 <- next normal sorting only by 
    when "Brand1 Model%" then 6
    else 7
end,

当然,品牌,型号和版本是一个单元格。确切地说,它是产品的名称。我不会爆炸产品名称。

2 个答案:

答案 0 :(得分:1)

如果name值模式固定为brandX modelY Type,则以下解决方案应该有效。

使用ORDER BY FIELDname拆分为所需的字词顺序。

示例

select name from (
  select name
   , @s1:=substring( name, locate( ' ', name )+1 ) s1 -- ModelY Premium/Normal/Standard
   , @s2:=substring_index( @s1, ' ', 1 ) s2           -- ModelY
   , @s3:=substring_index( @s1, ' ', -1 ) s3          -- Premium/Normal/Standard
  from table_name
) segregated_name_results
order by 
 field( s2, 'Model2', 'Model1', 'Model3' )
 , field( s3, 'Premium', 'Normal', 'Standard' )
;

+------------------------+
| name                   |
+------------------------+
| Brand1 Model2 Premium  |
| Brand1 Model2 Normal   |
| Brand1 Model2 Standard |
| Brand1 Model1 Premium  |
| Brand1 Model1 Normal   |
| Brand1 Model1 Standard |
| Brand1 Model3 Premium  |
| Brand1 Model3 Normal   |
| Brand1 Model3 Standard |
+------------------------+

子查询的参考值

select name
 , @s1:=substring( name, locate( ' ', name )+1 ) s1
 , @s2:=substring_index( @s1, ' ', 1 ) s2
 , @s3:=substring_index( @s1, ' ', -1 ) s3
from brand_models

+------------------------+-----------------+--------+----------+
| name                   | s1              | s2     | s3       |
+------------------------+-----------------+--------+----------+
| Brand1 Model1 Premium  | Model1 Premium  | Model1 | Premium  |
| Brand1 Model1 Standard | Model1 Standard | Model1 | Standard |
| Brand1 Model1 Normal   | Model1 Normal   | Model1 | Normal   |
| Brand1 Model2 Normal   | Model2 Normal   | Model2 | Normal   |
| Brand1 Model2 Premium  | Model2 Premium  | Model2 | Premium  |
| Brand1 Model2 Standard | Model2 Standard | Model2 | Standard |
| Brand1 Model3 Normal   | Model3 Normal   | Model3 | Normal   |
| Brand1 Model3 Premium  | Model3 Premium  | Model3 | Premium  |
| Brand1 Model3 Standard | Model3 Standard | Model3 | Standard |
+------------------------+-----------------+--------+----------+

演示 @ MySQL 5.5.32 Fiddle

答案 1 :(得分:0)

你可以这样做:

order by substring_index(name, ' ', 2),
         (case when substring_index(name, ' ', -1) = 'Premium' then 1
               when substring_index(name, ' ', -1) = 'Standard' then 2
               when substring_index(name, ' ', -1) = 'Normal' then 3
          end)

这假设特殊字符串(“premium”等)位于name的末尾,前两个单词足以对name列进行排序。