简体   繁体   English

SQL中的字符串模式匹配

[英]String pattern matching in sql

Please help me out in writing a generic SQL query for the below scenarios. 请帮我编写以下情况的通用SQL查询。 Based on the data in the table2 the output is decided.For Eg1, if AB* is present in table2,the output would be AB01,AB02. 根据表2中的数据确定输出。对于Eg1,如果表2中存在AB *,则输出为AB01,AB02。 Eg2 - AB02 is present in Table2, only AB02 is in output Eg3 - * is present in Table2, all the data in Table1 is in output Eg2-AB02在表2中,只有AB02在输出中Eg3--*在表2中存在,表1中的所有数据在输出中

Scenario 1 场景1

Table1  
AB01  
AB02  
BE01  
GH01  

Table2  
AB*

Output  
AB01  
AB02 

Scenario 2

Table1  
AB01  
AB02  
BE01  
GH01  

Table2  
AB02

Output   
AB02

Scenario 3 场景3

Table1

AB01  
AB02  
BE01  
GH01  

Table2  
*

Output  
AB01  
AB02  
BE01  
GH01  

Use rlike on a cross join between the tables: 在表之间的交叉rlike上使用rlike

select * 
  from table1 t1, table2 t2
  where t1.col1 rlike t2.col2;

You may need to tweak your expressions in table2 to become standard regex patterns, but that should be easy. 您可能需要调整table2中的表达式以成为标准的正则表达式模式,但这应该很容易。 See https://dev.mysql.com/doc/refman/8.0/en/regexp.html 参见https://dev.mysql.com/doc/refman/8.0/en/regexp.html

这应该为您工作:

select a.* from table1 a join table2 b on a.field like '%'+replace(b.field,'*','')+'%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM