具有混合列数据类型的顺序和大小写

时间:2018-04-09 18:50:17

标签: mysql sql-order-by

我们需要使用带有case子句的order by,因为用户可以从下拉列表中选择订单类型..情况似乎没问题,直到还引入了具有整数值wa的列..似乎由于某种原因,MYSQL也假设整数列是一个字符串并同样排序..

下面是order by子句

 order by (case when p_SortBy = 'Default' then CONVERT(st.IncrementNo, unsigned)
  WHEN  p_SortBy ='Program'
    THEN st.StudyProgram 
    WHEN p_SortBy = 'Student'
    then st.Name
    when p_SortBy = 'Mobile'
    then st.Mobile

  END) asc

第一种情况(默认)是一个整数值,但它是这样排序的 105,106,107,108,109,11,110

任何帮助表示赞赏..

1 个答案:

答案 0 :(得分:0)

您可以使用多个CASE

order by 
  case when p_SortBy = 'Default'  then CONVERT(st.IncrementNo, unsigned) END,
  case when p_SortBy = 'Program'  then st.StudyProgram END,
  case when p_SortBy = 'Student'  then st.Name END,
  case when p_SortBy = 'Mobile'   then st.Mobile END
相关问题