簡體   English   中英

如何在mysql中檢查列值是否具有字符串或數字值?

[英]How to check that a column values has string or digits values in mysql?

我有多列的mysql表,一列具有字符串數據類型,code並且該列的值可能僅是數字,或者是字符串和數字的混合,例如:一行的值是57677 ,其他行的列值是2cskjf89378732sdjfh

mysql> select * from testuser;
+------+--------------+
| id   | code         |
+------+--------------+
|    1 | 232          |
|    2 | adfksa121dfk |
|    3 | 12sdf        |
|    4 | dasd231      |
|    5 | 897          |
+------+--------------+
5 rows in set (0.00 sec)

mysql> show create table testuser;
+----------+------------------------------------------------------------------    ---------------------------------------------------------------+
   | Table    | Create Table                                                                                                                        |
+----------+------------------------------------------------------------------    ---------------------------------------------------------------+
| testuser | CREATE TABLE `testuser` (
  `id` int(11) DEFAULT NULL,
  `code` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------    ---------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

我想僅過濾出其具有數字值的行code ,其具有在字符串值列或行過濾器code列。

獲取數字

SELECT id,code 
FROM table_name
WHERE code REGEXP '^[0-9]+$';

獲取字符串值

SELECT * 
FROM table_name
WHERE code REGEXP '^[a-zA-Z]+$'

嘗試這個

MySQL查詢僅從特定列中獲取整數值

SELECT column FROM TABLE where column NOT REGEXP '^[0-9]+$' ;

MySQL查詢僅從特定列中獲取字符串值

SELECT column FROM TABLE where  column REGEXP '^[0-9]+$' ;

使用正則表達式

SELECT * 
FROM myTable 
WHERE code REGEXP '^[0-9]+$';

這將濾除所有數字的列值。

您也可以在mysql中使用正則表達式。

SELECT * FROM `table` WHERE `code` REGEXP '[0-9]';

結果將是行, code列中的任何數字。

嘗試這個,

select id,code from table_name where code NOT REGEXP '^[0-9]+$'

要么,

select id,code from table_name where code like '%[^0-9]%'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM