我得到的单元格可能具有以下数据(错误数量)

1
2
3
PASS
NoFileFound
NoLog
99
10
2


我想按升序和降序排序,在这种情况下,我会将PASS视为0的值,而将任何其他基于文本的值视为1的值。到目前为止,这些单元格已作为“文本”存储在mysql数据库中。如何为MYSQL做到这一点?我需要做什么改变?

最佳答案

试试这个:

SELECT Number FROM (
   SELECT IF(valueField='PASS',0,1) as Number  FROM TableMix
   WHERE concat('',valueField * 1) <> valueField ) A
UNION ALL
SELECT Number FROM (
  SELECT CAST(valueField as UNSIGNED) as Number  FROM TableMix
  WHERE concat('',valueField * 1) = valueField ) B
ORDER BY Number


见我的SqlFiddle Demo

而此原始值包括:

SELECT Number, valueField FROM (
   SELECT IF(valueField='PASS',0,1) as Number, valueField  FROM TableMix
   WHERE concat('',valueField * 1) <> valueField ) A
UNION ALL
SELECT Number, valueField FROM (
  SELECT CAST(valueField as UNSIGNED) as Number, valueField  FROM TableMix
  WHERE concat('',valueField * 1) = valueField ) B
ORDER BY Number


参见此Demo

07-27 20:01