2011-05-09 115 views
2

的我有以下表中的行:SQLite的全文搜索:查找countains任何关键字

-- 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的(第一关键字)冠军,所以关键字表格的其他条目将被忽略。

问题: 我怎么能找到包含任何指定关键字的所有文章? 谢谢。

回答

2

使用

select * 
    from articles_fts 
WHERE articles_fts MATCH (select group_concat(keyword, ' OR ') 
           from keywords 
          group by 'x') 

group_concat功能聚合所有的关键字用逗号。如果你用OR替换逗号,它应该产生一个很好的FTS查询。

有关OR关键字和reference for aggregate functions的另请参见the full text search feature reference的第3节。

+0

看起来不起作用:'错误:在GROUP BY子句中不允许使用聚合函数',如果我没有使用'GROUP BY 1'',没有错误,但查询返回没有行:( – 2011-05-09 08:09:56

+0

@Anton Prokoiev:我使用1作为常量,并忘记它被解释为列参考,尝试用'x'来代替 – Benoit 2011-05-09 09:22:28

+1

谢谢你的工作。唯一的一个说法:你的查询应该是这样的: '从articles_fts中选择articles_fts MATCH(从关键字组中选择group_concat(关键字,'OR')'x');'。**所以'OR'应该是UPPER CASE **在其他情况下它不作为关键字:) – 2011-05-09 10:28:52