使用MySQL REGEXP提取以特定數字開頭的字串 + 數字記錄?
為此,使用 REGEXP 並提取以特定數字開頭的記錄。以下為語法
Select yourColumnName1,yourColumnName2 from yourTableName where yourColumnName2 REGEXP '^yourStringValue[yourNumericValue]';
讓我們建立一個表 −
mysql> create table demo45 -> ( −> id int not null auto_increment primary key, −> value varchar(50) −> ); Query OK, 0 rows affected (1.50 sec)
運用 insert 命令向表中插入一些記錄。我們插入的是混合字串和數字的記錄,即“John500、”John6500”等等 −
mysql> insert into demo45(value) values('John500'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo45(value) values('John1500'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo45(value) values('John5500'); Query OK, 1 row affected (0.42 sec) mysql> insert into demo45(value) values('John6500'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo45(value) values('John8600'); Query OK, 1 row affected (0.19 sec)
使用 select 語句從表中顯示記錄 −
mysql> select *from demo45;
將產生下列輸出 −
+----+----------+ | id | value | +----+----------+ | 1 | John500 | | 2 | John1500 | | 3 | John5500 | | 4 | John6500 | | 5 | John8600 | +----+----------+ 5 rows in set (0.00 sec)
以下是提取帶有特定數字的記錄的查詢,此處為 5 和 6 −
mysql> select id,value −> from demo45 −> where value REGEXP '^John[56]';
將產生下列輸出 −
+----+----------+ | id | value | +----+----------+ | 1 | John500 | | 3 | John5500 | | 4 | John6500 | +----+----------+ 3 rows in set (0.00 sec)
廣告