如何在MySQL中正确使用%通配符?
您可以使用Like运算符来处理%通配符。语法如下
select *from yourTableName where yourColumName Like ‘condition%’;
为了理解上述概念,让我们首先创建一个表。查询创建表
mysql> create table SearchDemo -> ( -> Name varchar(100), -> LoginId varchar(100) -> );
使用insert命令在表中插入一些记录。查询如下
mysql> insert into SearchDemo values('John','1_1'); mysql> insert into SearchDemo values('Johnson','1_2'); mysql> insert into SearchDemo values('Carol','2_1'); mysql> insert into SearchDemo values('Bob','11_1'); mysql> insert into SearchDemo values('Sam','11_2'); mysql> insert into SearchDemo values('Mike','21_1');
使用select语句显示表中的所有记录。查询如下
mysql> select *from SearchDemo;
以下是输出
+---------+---------+ | Name | LoginId | +---------+---------+ | John | 1_1 | | Johnson | 1_2 | | Carol | 2_1 | | Bob | 11_1 | | Sam | 11_2 | | Mike | 21_1 | +---------+---------+ 6 rows in set (0.00 sec)
%是一种通配符,代表零个,一个或多个字符。使用%通配符查询如下
mysql> select *from SearchDemo -> where LoginId Like '2%';
以下是输出
+-------+---------+ | Name | LoginId | +-------+---------+ | Carol | 2_1 | | Mike | 21_1 | +-------+---------+ 2 rows in set (0.00 sec)