我会尝试用示例
更好地解释标题表1示例
Id text
1 lorem ipsum doe
2 foo bar lorem ipsum jhon
3 bla bla ipsum tommy
表2示例
Id fullname name surname keyword
1 jhon doe jhon doe jhon
2 tom asd tom asd tom
3 sam frf sam frr sam
使用like或regexp的预期表结果?
fullname count(*)
jhon doe 2
tom asd 1
sam frf 0
非常感谢!
答案 0 :(得分:5)
最简单的方法是使用REGEXP。
SELECT fullname,
Count(t1.id)
FROM table1 t1
RIGHT JOIN table2 t2
ON t1.text REGEXP t2.keyword
GROUP BY fullname
我使用了一个RIGHT连接,这样你就可以得到零的山姆(否则就会被淘汰)
答案 1 :(得分:3)
使用我的真实数据进行一些性能测试
t1 => 100,000行并且不断增长
t2 => 207行
测试1
SELECT
t2.fullname,
count(t1.id) AS total
FROM
table_1 AS t1
RIGHT JOIN
table_2 AS t2 ON t1.text REGEXP t2.keyword
GROUP BY t2.fullname
ORDER BY total DESC
212 seconds
测试2
SELECT
t2.fullname,
count(t1.id) AS total
FROM
table_1 AS t1
RIGHT JOIN
table_2 AS t2 ON t1.text LIKE CONCAT('%', t2.keyword, '%')
GROUP BY t2.fullname
ORDER BY total DESC
30 seconds
测试3
SELECT
t2.fullname,
count(t1.id) AS total
FROM
table_1 AS t1
RIGHT JOIN
table_2 AS t2 ON t1.text LIKE lower(CONCAT('%', t2.name, '%')) AND t1.text LIKE lower(CONCAT('%', t2.surname, '%'))
GROUP BY t2.fullname
ORDER BY total DESC
32 seconds
测试4
SELECT
t2.fullname,
count(t1.id) AS total
FROM
table_1 AS t1
RIGHT JOIN
table_2 AS t2 ON t1.text LIKE lower(CONCAT('%', t2.name, '%')) OR t1.text LIKE lower(CONCAT('%', t2.surname, '%'))
GROUP BY t2.fullname
ORDER BY total DESC
40 seconds
测试5
SELECT
t2.fullname,
count(t1.id) as total
FROM
table_1 as t1
RIGHT JOIN
table_2 as t2 ON t1.text LIKE CONCAT('%', t2.keyword, '%') OR (t1.text LIKE lower(CONCAT('%', t2.name, '%')) AND t1.text LIKE lower(CONCAT('%', t2.surname, '%')))
GROUP BY t2.fullname
ORDER BY total DESC
41 seconds
我会选择测试5。 最佳折衷结果/性能
还有什么建议吗?
再次感谢您的帮助!