MySQL에서 값이 숫자인지 감지
MySQL 쿼리에서 값이 숫자인지 감지하는 방법이 있습니까? 와 같은
SELECT *
FROM myTable
WHERE isANumber(col1) = true
대부분의 경우 작동합니다.
SELECT * FROM myTable WHERE concat('',col1 * 1) = col1
다음과 같은 비표준 숫자에는 작동하지 않습니다.
1e4
1.2e5
123.
(소수점)
정규 표현식도 사용할 수 있습니다 ...
SELECT * FROM myTable WHERE col1 REGEXP '^[0-9]+$';
참조 : http://dev.mysql.com/doc/refman/5.1/en/regexp.html
데이터가 'test', 'test0', 'test1111', '111test', '111'인 경우
데이터가 간단한 int 인 모든 레코드를 선택하려면 다음을 수행하십시오.
SELECT *
FROM myTable
WHERE col1 REGEXP '^[0-9]+$';
결과 : '111'
(정규식에서 ^는 시작을 의미하고 $는 끝을 의미합니다)
정수 또는 10 진수가있는 모든 레코드를 선택하려면 다음을 수행하십시오.
SELECT *
FROM myTable
WHERE col1 REGEXP '^[0-9]+\\.?[0-9]*$'; - for 123.12
결과 : '111'(마지막 예제와 동일)
마지막으로 숫자가 존재하는 모든 레코드를 선택하려면 다음을 사용하십시오.
SELECT *
FROM myTable
WHERE col1 REGEXP '[0-9]+';
결과 : 'test0'및 'test1111'및 '111test'및 '111'
이 답변은 Dmitry와 비슷하지만 소수점과 양수 및 음수를 허용합니다.
select * from table where col1 REGEXP '^[[:digit:]]+$'
숫자 행을 반환
나는 다음과 같은 쿼리로 해결책을 찾았고 나를 위해 일했다.
SELECT * FROM myTable WHERE col1 > 0;
이 쿼리는 숫자가 0보다 큰 행만 반환합니다. col1
숫자가 아닌 행을 반환
숫자가 아닌 열을 확인하려면 트릭 ( !col1 > 0
)으로 시도하십시오 .
SELECT * FROM myTable WHERE !col1 > 0;
SELECT * FROM myTable
WHERE col1 REGEXP '^[+-]?[0-9]*([0-9]\\.|[0-9]|\\.[0-9])[0-9]*(e[+-]?[0-9]+)?$'
-1.2, +0.2, 6., 2e9, 1.2e-10 과 같이 부호가있는 소수와도 일치 합니다.
테스트:
drop table if exists myTable;
create table myTable (col1 varchar(50));
insert into myTable (col1)
values ('00.00'),('+1'),('.123'),('-.23e4'),('12.e-5'),('3.5e+6'),('a'),('e6'),('+e0');
select
col1,
col1 + 0 as casted,
col1 REGEXP '^[+-]?[0-9]*([0-9]\\.|[0-9]|\\.[0-9])[0-9]*(e[+-]?[0-9]+)?$' as isNumeric
from myTable;
결과:
col1 | casted | isNumeric
-------|---------|----------
00.00 | 0 | 1
+1 | 1 | 1
.123 | 0.123 | 1
-.23e4 | -2300 | 1
12.e-5 | 0.00012 | 1
3.5e+6 | 3500000 | 1
a | 0 | 0
e6 | 0 | 0
+e0 | 0 | 0
내 컴퓨터에서 REGEXP보다 빠른 다른 대안은
SELECT * FROM myTable WHERE col1*0 != col1;
이것은 col1이 숫자 값으로 시작하는 모든 행을 선택합니다.
UDF (사용자 정의 함수)를 사용하십시오.
CREATE FUNCTION isnumber(inputValue VARCHAR(50))
RETURNS INT
BEGIN
IF (inputValue REGEXP ('^[0-9]+$'))
THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END;
그런 다음 쿼리하면
select isnumber('383XXXX')
--0을 반환
select isnumber('38333434')
--1을 반환
tablex에서 isnumber (mycol) mycol1, col2, colx를 선택하십시오. -mycol1 열에 대해 1과 0을 반환합니다.
--you can enhance the function to take decimals, scientific notation , etc...
The advantage of using a UDF is that you can use it on the left or right side of your "where clause" comparison. this greatly simplifies your SQL before being sent to the database:
SELECT * from tablex where isnumber(columnX) = isnumber('UnkownUserInput');
hope this helps.
Still missing this simple version:
SELECT * FROM myTable WHERE `col1` + 0 = `col1`
(addition should be faster as multiplication)
Or slowest version for further playing:
SELECT *,
CASE WHEN `col1` + 0 = `col1` THEN 1 ELSE 0 END AS `IS_NUMERIC`
FROM `myTable`
HAVING `IS_NUMERIC` = 1
I recommend: if your search is simple , you can use `
column*1 = column
` operator interesting :) is work and faster than on fields varchar/char
SELECT * FROM myTable WHERE column*1 = column;
ABC*1 => 0 (NOT EQU **ABC**)
AB15*A => 15 (NOT EQU **AB15**)
15AB => 15 (NOT EQU **15AB**)
15 => 15 (EQUALS TRUE **15**)
SELECT * FROM myTable WHERE sign (col1)!=0
ofcourse sign(0) is zero, but then you could restrict you query to...
SELECT * FROM myTable WHERE sign (col1)!=0 or col1=0
UPDATE: This is not 100% reliable, because "1abc" would return sign of 1, but "ab1c" would return zero... so this could only work for text that does not begins with numbers.
you can do using
CAST
SELECT * from tbl where col1 = concat(cast(col1 as decimal), "")
I have found that this works quite well
if(col1/col1= 1,'number',col1) AS myInfo
Try Dividing /1
select if(value/1>0 or value=0,'its a number', 'its not a number') from table
참고URL : https://stackoverflow.com/questions/5064977/detect-if-value-is-number-in-mysql
'IT story' 카테고리의 다른 글
jQuery 유효성 검사 필수 선택 (0) | 2020.06.18 |
---|---|
Brew를 사용하여 최신 버전의 Node를 설치하는 방법 (0) | 2020.06.18 |
Javascript-innerHTML없이 컨테이너 요소에 HTML 추가 (0) | 2020.06.18 |
몽구스 스키마에 created_at 및 updated_at 필드 추가 (0) | 2020.06.18 |
jasmine.js에서 하나의 사양에 집중하는 방법은 무엇입니까? (0) | 2020.06.17 |