order by and case与混合列数据类型

qnyhuwrf  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(316)

我们需要使用带有case子句的order by,因为用户可以从下拉列表中选择order的类型。。情况似乎还可以,直到还引入了一个整数值为wa的列。。似乎出于某种原因,mysql还假设整数列是字符串,并且排序也是一样的。。
以下是订货条款

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 感谢您的帮助。。

ctzwtxfj

ctzwtxfj1#

你可以用多个 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

相关问题