您可以使用以下语法或两个类似的语句-
SELECT *FROM yourTableName WHERE (yourColumnName like '%yourValue1%' OR yourColumnNamelike '%yourValue2%') AND yourColumnName = yourValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table ORLikeDemo -> ( -> Id int not null auto_increment, -> FirstName varchar(15), -> LastName varchar(15), -> Primary Key(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into ORLikeDemo(FirstName,LastName) values('John','Smith'); mysql> insert into ORLikeDemo(FirstName,LastName) values('Carol','Taylor'); mysql> insert into ORLikeDemo(FirstName,LastName) values('John','Doe'); mysql> insert into ORLikeDemo(FirstName,LastName) values('Bob','Jones'); mysql> insert into ORLikeDemo(FirstName,LastName) values('David','Miller'); mysql> insert into ORLikeDemo(FirstName,LastName) values('Mike','Williams'); mysql> insert into ORLikeDemo(FirstName,LastName) values('Sam','Taylor');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from ORLikeDemo;
以下是输出-
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 1 | John | Smith | | 2 | Carol | Taylor | | 3 | John | Doe | | 4 | Bob | Jones | | 5 | David | Miller | | 6 | Mike | Williams | | 7 | Sam | Taylor | +----+-----------+----------+ 7 rows in set (0.00 sec)
这是使用两个LIKE和OR运算符的查询-
mysql> select *from ORLikeDemo where (LastName like '%Smith%' OR LastName like'%Doe%') AND Id = 3;
以下是输出-
+----+-----------+----------+ | Id | FirstName | LastName | +----+-----------+----------+ | 3 | John | Doe | +----+-----------+----------+ 1 row in set (0.03 sec)