我需要一些帮助来计算记录标题字段中的多个特定单词。
所以这是我的查询
SELECT
SUM(CASE WHEN `title` LIKE '%fish%' THEN 1 ELSE 0 END) AS count1,
FROM tbl_recipes
GROUP BY recipeID
这是有效但如果我想计算多个单词我不知道如何实现这一点。
我尝试过这样的事情
SELECT
SUM(CASE WHEN title LIKE '%fish%' OR WHEN title LIKE '%soup%' THEN 1 ELSE 0 END) AS count1,
FROM tbl_recipes
GROUP BY recipeID
但这引发了异常,是否有人知道计算数据库字段中特定单词的多个实例的正确方法。
最多可能有6个单词需要计数,我需要将它们全部计算为相同的计数而不是单独计算:)
由于
答案 0 :(得分:0)
对于个人帐户,您必须尝试以下方式: -
SELECT
SUM(CASE WHEN title LIKE '%fish%' THEN 1 ELSE 0 END) AS count1,
SUM(CASE WHEN title LIKE '%soup%' THEN 1 ELSE 0 END) AS count2,
SUM(CASE WHEN title LIKE '%fish%' THEN 1 ELSE 0 END) AS count1 +
SUM(CASE WHEN title LIKE '%soup%' THEN 1 ELSE 0 END) AS count2 AS TOTAL
FROM tbl_recipes
GROUP BY recipeID