如何在mysql中将类型从varchar转换为int来选择min

如何在mysql中将类型从varchar转换为int来选择min

问题描述:

如何在mysql中将varchar转换为双精度值

How to convert varchar into double value in mysql

看,我在varchar中有表列,但它只有数字.在这种情况下,我想选择该值的最小最大值.

See, i have table column in varchar but it has only numbers. In that i want to select min max of the value.

请检查以下查询,其中出现语法错误.

Please check the below query, in which I'm getting a syntax error.

select 
    MAX(CAST(ch1 as INT)) as max_ch1,
    MIN(CAST(ch1 as INT)) as min_ch1 
from t9;

请参考以下 sqlfiddle

我认为这是您要寻找的:

I think this is what you are looking for:

SELECT
  MIN(CAST(CH1 AS SIGNED)),
  MAX(CAST(CH1 AS SIGNED))
FROM t9

正在使用SQLFiddle 此处.

Working SQLFiddle here.

您必须将值CAST投射为SIGNED,它对应于MySQL中的INTEGER.关于此的更多信息,请此处.

You have to CAST the value as SIGNED, which corresponds to INTEGER in MySQL. More information about this, here.