所以我尝试实现的是连接两个表,其中要连接的列需要regexp以将两个表匹配在一起。我曾经有一个LIKE(%)的解决方案,但现在我想尝试通过匹配regexp来加快查询速度。
table1
| col1 | col2 |
| blablaaa |12323_string|
table2
| col2 | col2 |
| blabla | string |
所以col2上的字符串是我想要加入它们的地方但我需要在下划线后匹配字符串......
这是我最接近的,我仍然可能会离开
SELECT *
FROM table1 AS t1 JOIN table2 AS t2
ON t1.col2 REGEXP CONCAT('(?<=_)[^_] + t2.col2')
我不知道是否有一个温暖的灵魂可以帮助我。
答案 0 :(得分:1)
只需使用substring_index()
:
SELECT *
FROM table1 t1 JOIN
table2 t2
ON t2.col2 = substring_index(t1.col2, '_', -1);
或者like
:
SELECT *
FROM table1 t1 JOIN
table2 t2
ON t1.col2 LIKE CONCAT('%\_', t2.col2);