您可以为此使用正则表达式。让我们首先创建一个表 -
mysql> create table DemoTable ( ProductId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductName varchar(20) );
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable(ProductName) values('Product-1'); mysql> insert into DemoTable(ProductName) values('Product2'); mysql> insert into DemoTable(ProductName) values('Product1'); mysql> insert into DemoTable(ProductName) values('Product-3'); mysql> insert into DemoTable(ProductName) values('Product3'); mysql> insert into DemoTable(ProductName) values('Product-4'); mysql> insert into DemoTable(ProductName) values('Product4');
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+-----------+-------------+ | ProductId | ProductName | +-----------+-------------+ | 1 | Product-1 | | 2 | Product2 | | 3 | Product1 | | 4 | Product-3 | | 5 | Product3 | | 6 | Product-4 | | 7 | Product4 | +-----------+-------------+ 7 rows in set (0.00 sec)
以下是对具有多个匹配字符串的分组记录的查询 -
mysql> select *from DemoTable where ProductName regexp 'Product[1234].*';
这将产生以下输出 -
+-----------+-------------+ | ProductId | ProductName | +-----------+-------------+ | 2 | Product2 | | 3 | Product1 | | 5 | Product3 | | 7 | Product4 | +-----------+-------------+ 4 rows in set (0.00 sec)