您可以將 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中文網其他相關文章!