gpt4 book ai didi

php - 计算匹配词的数量

转载 作者:行者123 更新时间:2023-11-29 02:26:48 26 4
gpt4 key购买 nike

我有两个表,数据填充在这个 sqlFiddle

现在,我有一个如下所示的查询,当我搜索“George Tabuki Street Fighter Miley Cyrus”时,我有 php explode 搜索字符串并动态构建查询添加 + CASE WHEN ... END

SELECT id,word,LEFT(description,100)as description, 
IFNULL((SELECT sum(vote)
FROM vote v
WHERE v.definition_id = d.id),0) as votecount,
0
+ CASE WHEN LOCATE('George',CONCAT(word,description,`usage`))>0 THEN 1 ELSE 0 END
+ CASE WHEN LOCATE('Tabuki',CONCAT(word,description,`usage`))>0 THEN 1 ELSE 0 END
+ CASE WHEN LOCATE('Street',CONCAT(word,description,`usage`))>0 THEN 1 ELSE 0 END
+ CASE WHEN LOCATE('Fighter',CONCAT(word,description,`usage`))>0 THEN 1 ELSE 0 END
+ CASE WHEN LOCATE('Miley',CONCAT(word,description,`usage`))>0 THEN 1 ELSE 0 END
+ CASE WHEN LOCATE('Cyrus',CONCAT(word,description,`usage`))>0 THEN 1 ELSE 0 END
as `match`
FROM definition d
HAVING `match` > 0
ORDER BY `match` DESC,votecount DESC

上面的查询返回的正是我想要的。

问题:有没有更好的方法,或者mySQL中有没有返回匹配词个数的函数?

更新:

我找到了一个更好的方法,但不是更好的方法,但它会返回匹配项的出现次数

SELECT id,word,LEFT(description,100)as description, 
IFNULL((SELECT sum(vote)
FROM vote v
WHERE v.definition_id = d.id),0) as votecount,
0
+ IFNULL(ROUND((LENGTH(CONCAT(word,description,`usage`,`by`)) - LENGTH(REPLACE(CONCAT(word,description,`usage`,`by`), 'George', '')))/LENGTH('George')),0)
+ IFNULL(ROUND((LENGTH(CONCAT(word,description,`usage`,`by`)) - LENGTH(REPLACE(CONCAT(word,description,`usage`,`by`), 'Tabuki', '')))/LENGTH('Tabuki')),0)
+ IFNULL(ROUND((LENGTH(CONCAT(word,description,`usage`,`by`)) - LENGTH(REPLACE(CONCAT(word,description,`usage`,`by`), 'Street', '')))/LENGTH('Street')),0)
+ IFNULL(ROUND((LENGTH(CONCAT(word,description,`usage`,`by`)) - LENGTH(REPLACE(CONCAT(word,description,`usage`,`by`), 'Fighter', '')))/LENGTH('Fighter')),0)
+ IFNULL(ROUND((LENGTH(CONCAT(word,description,`usage`,`by`)) - LENGTH(REPLACE(CONCAT(word,description,`usage`,`by`), 'Miley', '')))/LENGTH('Miley')),0)
+ IFNULL(ROUND((LENGTH(CONCAT(word,description,`usage`,`by`)) - LENGTH(REPLACE(CONCAT(word,description,`usage`,`by`), 'Cyrus', '')))/LENGTH('Cyrus')),0)
as `match`
FROM definition d
HAVING `match` > 0
ORDER BY `match` DESC,votecount DESC;

最佳答案

不确定这是否是更好的方法,但我会这样做:

SELECT d.id, d.word, LEFT(d.description, 100) description,
COALESCE(sum(v.vote), 0) votecount,
(CONCAT(word, description, `usage`) LIKE '%George%')
+ (CONCAT(word, description, `usage`) LIKE '%Tabuki%')
+ (CONCAT(word, description, `usage`) LIKE '%Street%')
+ (CONCAT(word, description, `usage`) LIKE '%Fighter%')
+ (CONCAT(word, description, `usage`) LIKE '%Miley%')
+ (CONCAT(word, description, `usage`) LIKE '%Cyrus%') `match`
FROM definition d
LEFT JOIN vote v ON v.definition_id = d.id
GROUP BY d.id
HAVING `match` > 0
ORDER BY `match` DESC, votecount DESC

如果字符串足够长,重复连接可能比创建派生表花费更多时间(不太可能,但值得一试):

SELECT id, word, description, votecount,
(fullDesc LIKE '%George%')
+ (fullDesc LIKE '%Tabuki%')
+ (fullDesc LIKE '%Street%')
+ (fullDesc LIKE '%Fighter%')
+ (fullDesc LIKE '%Miley%')
+ (fullDesc LIKE '%Cyrus%') `match`
FROM (
SELECT d.id, d.word, LEFT(d.description, 100) description,
COALESCE(sum(vote), 0) votecount, CONCAT(word, description, `usage`) fullDesc
FROM definition d
LEFT JOIN vote v ON v.definition_id = d.id
GROUP BY d.id
) s
HAVING `match` > 0
ORDER BY `match` DESC, votecount DESC

关于php - 计算匹配词的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20320380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com