如何在mysql选择查询中检查值是数字还是非数字,如下所示

时间:2015-06-29 06:13:38

标签: mysql

值 - > {1,2,是,否,5,6}

select if((value is numeric),value,'not a numeric') as column_name 

如果在我的mysql选择查询中如何实现这个

3 个答案:

答案 0 :(得分:5)

这应该这样做:)

select if(field REGEXP '^-?[0-9]+$' > 0, field, 'not a numeric') as column_name

示例:

SELECT '12345' REGEXP '^-?[0-9]+$'  

返回:1(它的数字)

SELECT 'abcdef' REGEXP '^-?[0-9]+$'  

返回:0(不是数字)

答案 1 :(得分:0)

你可以试试 -

SELECT IF(table_column*1>0,table_column,'Not a Numeric') 
FROM your_table;

注意:如果有一个值为“9 hello”,那么它将被视为数字,因为9是数值,如果您需要将其排除,则还原。但在你的问题中没有这样的价值。

答案 2 :(得分:0)

这是一个老问题,但当我需要同样的东西时,最快的解决方案是

set.seed(42)
N <- 1000
df <- data.frame(start=sample.int(10*N, N))
df$end <- df$start + sample(3:20, N, rep=TRUE) 

library("microbenchmark")
microbenchmark(unit = "relative",
ori =  { positions <- c()
  for(i in seq(nrow(df))){
    positions <- c(positions, seq(df[i,1], df[i,2]))
  }
  table(positions) },
a  = table(unlist(apply(df, 1, function(x) x[1]:x[2]))),  # my solution, similar: KenS, EricSchutte
m1 = table(unlist(mapply(seq, df$start, df$end))),        # my variant of Sotos' solution
m2 = table(unlist(mapply(':', df$start, df$end))),        # my variant of Sotos' solution
M1 = table(unlist(Map(seq, df$start, df$end))),           # my variant of Sotos' solution
M2 = table(unlist(Map(':', df$start, df$end))),           # Sotos
l  = table(unlist(lapply(seq_len(nrow(df)), function(i) seq(df$start[i], df$end[i])))),    # lmo
t  = { temp <- unlist(lapply(seq_len(nrow(df)), function(i) seq(df$start[i], df$end[i])))  # lmo tabulate()
cbind(sort(unique(temp)), tabulate(temp)) },
d  = table(do.call(c, mapply(seq, df$start, df$end))),     # @989 (comment to the answer from Sotos)
dd = table(do.call(c, mapply(seq.int, df$start, df$end))), # docendo discimus (comment to this answer)
f  = {  pos <- data.frame(x=(min(df):max(df)),n=0)         # Andrew Gustar

for(i in seq_along(df$start)){
  low=min(df$start[i])-pos$x[1]+1
  high=max(df$end[i])-pos$x[1]+1
  pos$n[low:high] <- pos$n[low:high]+1
} }
)
# Unit: relative
# expr      min       lq     mean   median       uq       max neval    cld
#  ori 7.163767 7.219099 7.573688 7.379160 7.912435  7.899586   100     e 
#    a 1.194627 1.194855 1.211432 1.209485 1.213118  1.711994   100 a     
#   m1 1.645659 1.660294 1.711141 1.686973 1.710461  2.217141   100  b    
#   m2 1.005302 1.007125 1.017115 1.009618 1.017207  1.576201   100 a     
#   M1 1.642688 1.645174 1.733173 1.673924 1.686253  2.218028   100  b    
#   M2 1.000000 1.000000 1.000000 1.000000 1.000000  1.000000   100 a     
#    l 3.487924 3.512732 3.801530 3.665725 4.188701  4.216375   100    d  
#    t 2.670636 2.711345 2.961449 2.869190 3.066150  3.745984   100   c   
#    d 1.652376 1.650798 1.721377 1.665901 1.712064  2.187129   100  b    
#   dd 1.040941 1.045652 1.060601 1.047534 1.053305  1.592163   100 a     
#    f 8.287098 8.486854 9.052884 9.046376 9.126318 25.210722   100      f

当然,这不会考虑零数字,但罗马人也没有,他们几千年来都没事。对我来说,考虑到数亿行的速度提高,这是最好的解决方案。