MySQL - 仅从varchar列中选择数值

时间:2014-03-19 15:32:03

标签: mysql sql numeric varchar

考虑下表:

create table mixedvalues (value varchar(50));

insert into mixedvalues values ('100');
insert into mixedvalues values ('ABC100');
insert into mixedvalues values ('200');
insert into mixedvalues values ('ABC200');
insert into mixedvalues values ('300');
insert into mixedvalues values ('ABC300');
insert into mixedvalues values ('400');
insert into mixedvalues values ('ABC400');
insert into mixedvalues values ('500');
insert into mixedvalues values ('ABC500');

如何编写一个只返回数字值的select语句,如

100
200
300
400
500

SQLFiddle

6 个答案:

答案 0 :(得分:72)

SELECT * 
FROM mixedvalues 
WHERE value REGEXP '^[0-9]+$';

答案 1 :(得分:4)

你很亲密:

SELECT * 
FROM mixedvalues 
WHERE value > 0;

SQLFiddle

答案 2 :(得分:4)

SELECT * 
FROM mixedvalues 
WHERE concat('',value * 1) = value;

参考:Detect if value is number in MySQL

答案 3 :(得分:1)

列表项的字符串以数字开头

$string = "Test";

select * from table where columnname REGEXP "$string-*[0-9]+"; 

答案 4 :(得分:0)

SELECT * FROM mixedvalues 
WHERE value > 0 
ORDER BY CAST(value as SIGNED INTEGER) ASC

答案 5 :(得分:-3)

您可以使用ISNUMERIC功能过滤结果集:

SELECT value
FROM #mixedvalues 
where ISNUMERIC(value)=1