您可以将 LIKE 与 OR 运算符一起使用,其作用与 IN 运算符相同。
让我们看看这两种情况的语法 -
select *from yourTableName where yourColumnName Like ‘Value1’ or yourColumnName Like ‘Value2’ or yourColumnName Like ‘Value3’ . . . N
select *from yourTableName where IN(value1,value2,value3,.....N);
为了理解这两种语法,让我们创建一个表。创建表的查询如下 -
mysql> create table LikeDemo −> ( −> Id varchar(20) −> ); Query OK, 0 rows affected (0.58 sec)
现在您可以借助insert语句在表中插入记录。查询如下 -
mysql> insert into LikeDemo values('John123'); Query OK, 1 row affected (0.22 sec) mysql> insert into LikeDemo values('Smith205'); Query OK, 1 row affected (0.18 sec) mysql> insert into LikeDemo values('Bob999'); Query OK, 1 row affected (0.18 sec) mysql> insert into LikeDemo values('Carol9091'); Query OK, 1 row affected (0.17 sec) mysql> insert into LikeDemo values('Johnson2222'); Query OK, 1 row affected (0.15 sec) mysql> insert into LikeDemo values('David2345'); Query OK, 1 row affected (0.21 sec)
借助 select 语句显示表中的所有记录。查询如下 -
mysql> select *from LikeDemo;
以下是输出 -
+-------------+ | Id | +-------------+ | John123 | | Smith205 | | Bob999 | | Carol9091 | | Johnson2222 | | David2345 | +-------------+ 6 rows in set (0.00 sec)
以下是使用单个 Like 和 OR 运算符的查询 -
mysql> select *from LikeDemo where Id Like 'John123%' or Id Like 'Carol9091%' or Id Like 'David2345%';
以下是输出 -
+-----------+ | Id | +-----------+ | John123 | | Carol9091 | | David2345 | +-----------+ 3 rows in set (0.00 sec)
查询如下 -
mysql> select *from LikeDemo where Id in('John123','Carol9091', 'David2345');
以下是输出 -
+-----------+ | Id | +-----------+ | John123 | | Carol9091 | | David2345 | +-----------+ 3 rows in set (0.04 sec)
以上是我们可以在MySql中同时使用LIKE和OR吗?的详细内容。更多信息请关注PHP中文网其他相关文章!