mysql - SQL:每行的用户变量增量

标签 mysql sql variables case increment

编辑:我的问题不清楚,所以我在这里重新表述了它:Order sql result by occurrence of a set of keywords in a string

我正在改进我网站的搜索系统。我正在尝试在 sql 请求中使用和递增变量,就像那样...

SET @titlematch = 0;
SELECT *,
CASE 
    when title like '%apple%' then (SET @titlematch = @titlematch+1)
    when title like '%orange%' then (SET @titlematch = @titlematch+1)
    when title like '%other_keyword_searched%' then (SET @titlematch = @titlematch+1)
    (...)
END,
(...)
FROM pages  
(...)
ORDER by @titlematch desc

事实上,每次标题中出现关键字时,titlematch 都应该递增。如果标题中有“apple”和“orange”,则 titlematch 应等于 2。 但实际上,它不起作用......

(对不起我的英语)

最佳答案

我认为它失败是因为它必须处理所有数据,如果标题像 someWordYouDontAcccountFor 它将失败。您必须考虑所有可能的情况或使用其他。

关于mysql - SQL:每行的用户变量增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18570263/

相关文章:

javascript - 无法使用括号中的变量从 JavaScript 数组获取值

php - echo html 和变量的语法帮助

php - 使用 Group By 时,PDO MySql 中不允许出现无效参数号问题

java - 将 UTC JodaTime.DateTime 对象转换为另一个时区的时间戳

mysql - 更改表添加新字段 "ON UPDATE CURRENT_TIMESTAMP"

MYSQL 从列匹配的表中选择

mysql - AWS Aurora MySQL 上的聚合查询非常慢

sql - Like 从句的模式匹配

r - 如何在 R 中传递字符串作为列名

c# - 如何从mysql日期时间字段中读取