![](/img/trans.png)
[英]MySQL select rows from a table where column is linked to another column in same table
[英]mysql select count rows from a table where column like values from another table
我将尝试用一个例子来更好地解释标题
表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
非常感谢!
最简单的是使用REGEXP。
SELECT fullname,
Count(t1.id)
FROM table1 t1
RIGHT JOIN table2 t2
ON t1.text REGEXP t2.keyword
GROUP BY fullname
我使用了一个正确的连接,这样你就可以得到零的山姆(否则就会被淘汰)
用我的真实数据进行一些性能测试
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.最佳折衷结果/性能
还有什么建议吗?
再次感谢你的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.