如何在mysql中通过变量排序?

时间:2015-01-02 11:51:26

标签: mysql sql select sql-order-by

我想以特定的方式订购我的桌子。

MYTABLE
第1列不同的值:

completed,in-progress,yetstart,yetfinish

我想通过

订购
yetstart,in-progress,completed,yetfinish  

按此顺序

3 个答案:

答案 0 :(得分:3)

FIELD()功能用于 ORDER BY 列数据:

试试这个:

SELECT * 
FROM tableA 
ORDER BY FIELD(column1, 'yetstart', 'in-progress', 'completed', 'yetfinish');

答案 1 :(得分:0)

select * from your_table
order by case when your_column = 'yetstart' then 1 
              when your_column = 'in-progress' then 2 
              when your_column = 'completed' then 3
              when your_column = 'yetfinish' then 4
              else 5
         end

答案 2 :(得分:0)

SELECT * FROM table_name
  ORDER BY field_name1 DESC, field_name2 DESC;