如何检查字符串是否包含 case 语句中的单词 - SQLite

Tha*_*tas 4 sql sqlite string substring case

这是我的代码:

SELECT 
CASE your_text
  WHEN contains('hate') THEN 'hater'
  WHEN contains('love') THEN 'lover'
  ELSE 'other'
END haterOrLover,
count(*) AS total_count
FROM a_table
GROUP BY haterOrLover
Run Code Online (Sandbox Code Playgroud)

如果只有 contains() 存在就好了!

我想计算有多少个仇恨者、爱好者,结果是这样的:

+--------------+-------------+  
| haterOrLover | total_count |  
+--------------+-------------+  
| hater        | 1           |  
+--------------+-------------+  
| lover        | 2           |  
+--------------+-------------+  
| other        | 1           |  
+--------------+-------------+
Run Code Online (Sandbox Code Playgroud)

尝试过:

+--------------+-------------+  
| haterOrLover | total_count |  
+--------------+-------------+  
| hater        | 1           |  
+--------------+-------------+  
| lover        | 2           |  
+--------------+-------------+  
| other        | 1           |  
+--------------+-------------+
Run Code Online (Sandbox Code Playgroud)
WHEN your_text like '%hate%' THEN 'hater'
Run Code Online (Sandbox Code Playgroud)

但没有用。

如何检查记录的“your_text”列中是否包含特定单词?

编辑
如何重现:

WHEN '%hate%' THEN 'hater'
Run Code Online (Sandbox Code Playgroud)

谢谢


解决方案

CREATE TABLE a_table
(
id         char(10)      PRIMARY KEY,
your_text  char(250)     NOT NULL
);

INSERT INTO a_table
    (id, your_text)
VALUES
    (1, 'I love dogs'),
    (2, 'I hate cats'),
    (3, 'I like rabits'),
    (4, 'I love haskis');
Run Code Online (Sandbox Code Playgroud)

小智 6

尝试一下 :

with cte as (
SELECT 
CASE when your_text like ('%hate%') THEN 'hater'
     when your_text like ('%love%') THEN 'lover'
     ELSE 'other'
     END haterOrLover FROM a_table )
select haterOrLover , count(*) as total_count from cte
group by (haterOrLover)

Run Code Online (Sandbox Code Playgroud)