请帮助我为以下场景编写通用SQL查询。 根据表2中的数据决定输出。对于 Eg1,如果表 2 中存在 AB*,则输出将为 AB01,AB02。 Eg2 - AB02 存在于表 2 中,只有 AB02 在输出中 Eg3 - * 存在于表 2 中,表 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
在表之间的交叉连接上使用rlike
:
select *
from table1 t1, table2 t2
where t1.col1 rlike t2.col2;
您可能需要调整 table2 中的表达式以成为标准的正则表达式模式,但这应该很容易。见 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,'*','')+'%'