我有一个数据库表:
Column Type
source text
news_id int(12)
heading text
body text
source_url tinytext
time timestamp
news_pic char(100)
location char(128)
tags text
time_created timestamp
hits int(10)现在,我正在搜索一个算法或工具,以便在这个表中搜索包含新闻数据的关键字。关键字应该在标题、正文、标签和新闻点击次数中搜索,以获得最佳效果。
发布于 2012-11-21 23:59:46
MySQL已经有了您需要内置的工具:全文搜索。我将假设您知道如何使用PHP与MySQL交互。如果没有,那就先调查一下。总之..。
1)向要搜索的字段添加全文索引:
alter table TABLE_NAME add fulltext(heading);
alter table TABLE_NAME add fulltext(body);
alter table TABLE_NAME add fulltext(tags);2)使用match ... against语句执行全文搜索:
select * from TABLE_NAME where match(heading, body, tags, hits) against ('SEARCH_STRING');显然,在这些示例中,将表的名称替换为TABLE_NAME,将搜索字符串替换为SEARCH_STRING。
我不明白你为什么要搜索点击次数,因为它只是一个整数。但是,通过在查询中添加order子句,可以按点击次数进行排序:
select * from TABLE_NAME where match(heading, body, tags, hits) against ('SEARCH_STRING') order by hits desc;https://stackoverflow.com/questions/13503798
复制相似问题