如何在 LIKE 运算符中引入多个条件? [英] How can I introduce multiple conditions in LIKE operator?
本文介绍了如何在 LIKE 运算符中引入多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写一个如下的SQL语句:
I want to write an SQL statement like below:
select * from tbl where col like ('ABC%','XYZ%','PQR%');
我知道可以使用 OR
来完成.但我想知道有没有更好的解决方案.
I know it can be done using OR
. But I want to know is there any better solution.
推荐答案
这是另一种方式:
select * from tbl where col like 'ABC%'
union
select * from tbl where col like 'XYZ%'
union
select * from tbl where col like 'PQR%';
这是要验证的测试代码:
Here is the test code to verify:
create table tbl (col varchar(255));
insert into tbl (col) values ('ABCDEFG'), ('HIJKLMNO'), ('PQRSTUVW'), ('XYZ');
select * from tbl where col like 'ABC%'
union
select * from tbl where col like 'XYZ%'
union
select * from tbl where col like 'PQR%';
+----------+
| col |
+----------+
| ABCDEFG |
| XYZ |
| PQRSTUVW |
+----------+
3 rows in set (0.00 sec)
这篇关于如何在 LIKE 运算符中引入多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文