SQLite全文搜索:查找包含任何关键字的行
我有以下表格:SQLite全文搜索:查找包含任何关键字的行,sqlite,full-text-search,Sqlite,Full Text Search,我有以下表格: -- table to keep articles titles CREATE TABLE articles(id, title); insert into articles (id,title) values (1,'sqlite example'); insert into articles (id,title) values (2,'sqlite query '); insert into articles (id,title) values (3,'erlang exam
-- table to keep articles titles
CREATE TABLE articles(id, title);
insert into articles (id,title) values (1,'sqlite example');
insert into articles (id,title) values (2,'sqlite query ');
insert into articles (id,title) values (3,'erlang example ');
insert into articles (id,title) values (3,'erlang otp ');
-- table to keep keywords that we would like to search.
create table keywords(id,keyword);
insert into keywords (id,keyword) values (1,'sqlite');
insert into keywords (id,keyword) values (2,'otp');
-- full text search table - copy of articles.
create virtual table articles_fts using fts4 (id,name);
-- populate table with articles titles.
insert into articles_fts(id,name) select id,title from articles;
现在我想查找所有包含任何指定关键字的文章
查询方式如下:
select * from articles_fts
where name match (select keyword from keywords);
只返回第一个关键字中包含sqlite的标题,因此忽略关键字表的其他条目
问题:
如何找到包含任何指定关键字的所有文章?
谢谢。使用
select *
from articles_fts
WHERE articles_fts MATCH ( select group_concat(keyword, ' OR ')
from keywords
group by 'x' )
group_concat函数使用逗号聚合所有关键字。如果用或替换逗号,则应生成良好的FTS查询
另请参阅关于OR关键字的第3节。看起来它不起作用:“错误:GROUP BY子句中不允许使用聚合函数”,如果我在没有GROUP BY 1的情况下执行,则不会出现错误,但查询不会返回任何行:@Anton Prokoiev:我使用GROUP BY 1作为常量,但忘记了它被解释为列引用,尝试“x”分组,而不是感谢它的工作。唯一一句话:您的查询应该是这样的:选择*来自文章,其中文章匹配选择组\u concat关键字,'或'来自关键字组,按'x';。所以“OR”应该是大写,在其他情况下,它不作为关键字插入: