sql中的字符串模式匹配

juud5qan  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(421)

请帮助我为以下场景编写一个通用sql查询。根据表2中的数据决定输出。对于eg1,如果表2中存在ab*,则输出为ab01、ab02。表2中有eg2-ab02,只有ab02在输出中。表2中有eg3-*,表1中的所有数据都在输出中
情景1

Table1  
AB01  
AB02  
BE01  
GH01  

Table2  
AB*

Output  
AB01  
AB02
Scenario 2

Table1  
AB01  
AB02  
BE01  
GH01  

Table2  
AB02

Output   
AB02

情景3

Table1

AB01  
AB02  
BE01  
GH01  

Table2  

* 

Output  
AB01  
AB02  
BE01  
GH01
sc4hvdpw

sc4hvdpw1#

使用 rlike 在表之间的交叉联接上:

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

您可能需要调整表2中的表达式以成为标准regex模式,但这应该很容易。看到了吗https://dev.mysql.com/doc/refman/8.0/en/regexp.html

23c0lvtd

23c0lvtd2#

这应该适合您:

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

相关问题