0

您好,我有一个可以包含字符串或数字的索引字符串字段。我想得到这个索引的最大值和最小值,但我希望一个只有数字的索引是整数,否则是字符串。例如我想要:

   ( "1", "5", "10" )       MIN=1 MAX=10 // mind MAX=10 not 5 as string order!
   ( "A", "D", "Z" )        MIN=A MAX=Z

我试过这个:

MAX(IF(CAST(idxfield as UNSIGNED)=0,CAST(idxfield as UNSIGNED),idxfield)) as maxField

但不起作用。

4

2 回答 2

0

尝试这个 ::

Select 
idxfield REGEXP '[0-9] as field1,
CASE field1
 WHEN 1 THEN SELECT idxfield from table order by idxfield+0 desc
 WHEN 0 THEN SELECT idxfield from table order by idxfield desc
END as max_value,
CASE field1
 WHEN 1 THEN SELECT idxfield from table order by idxfield+0
 WHEN 0 THEN SELECT idxfield from table order by idxfield
END as min_value
from table
于 2012-06-21T17:39:45.300 回答
0

您可以REGEXP用来测试该字段是否为数字。

例如:

select '1'  REGEXP '^[0-9]+$'; // returns 1
select 'A'  REGEXP '^[0-9]+$'; // returns 0
于 2012-06-21T15:55:12.343 回答