进行查询以选择包含特定单词的新闻

Making a query which selects the news that contain a specific word

       CREATE TABLE IF NOT EXISTS news (
           title TEXT PRIMARY KEY,
           newsletter_description TEXT NOT NULL
       );

我需要编写一个查询,选择所有在 titlenewsletter_description 中包含单词“apple”或“watermelon”(或两者)的 news我不太确定我该怎么做。 (不区分大小写,也可以是“AppLe”或“WaterMelon”)

您可以使用 like 运算符并进行不区分大小写的搜索,您可以在实际列上使用 lowerupper 并且还必须将输入转换为 lower/upper 在相应地传递给查询之前,

select * 
  from news 
 where lower(newsletter_description) like '%watermelon%' 
    or lower(newsletter_description) like '%apple%'
    or lower(title) like '%watermelon%' 
    or lower(title) like '%apple%';
    

您可以使用 « lower(title) like '%apple%' » 其实下面把所有的字段都放在很小的地方,这样可以帮助你在不知道他是怎么写的情况下找到需要的单词

SELECT * FROM NEWS
WHERE title LIKE "%apple%" OR 
      title LIKE "%watermelon%" OR
      newsletter_description LIKE "%apple%" OR 
      newsletter_description LIKE "%watermelon% 

SQlite 默认实现了对 ASCII 字符不区分大小写的 LIKE 运算符。除非您在文本中使用 unicode 字符,否则您可以使用上述查询。

但是,如果您使用 unicode 字符,则使用 lower 或 upper 函数也不起作用。所以根本没有必要使用 lower 或 upper 函数。

https://www.sqlite.org/c3ref/strlike.html

使用 returns 您搜索的所有词的 CTE,并将其加入 table:

with cte(word) as (values ('apple'), ('watermelon'))
select n.*
from news n inner join (select '%' || word || '%' word from cte) c
on n.title like c.word or n.newsletter_description like c.word

天真的方法是 select * 从新的 where lower(title) like '%apple%' or lower(title) like '%watermelon%' or lower(newsletter_description) like ' %apple%' 或更低(newsletter_description)如'%watermelon%';