首页 > Mysql 多表搜索

Mysql 多表搜索

情况是这样的,一个项目有多个栏目:问答、商城、文章,也分别有三个表,ask, shopping, article。如何使用一个关键字搜索三个表里的内容,并实现时间排序?


你这样每次搜索都查几张表性能值得考虑, 感觉用solr之类的搜索框架比较好吧


上面被采纳的回答说的结构统一应该不是说表的结构统一 是结果字段统一
例如:
select id, t.** title, t.** create_time
from ask t
where 1=1
    and t.** like '%%'
union all
select id, t.** title, t.** create_time
from shopping t
where 1=1
    and t.** like '%%'
union all
select id, t.** title, t.** create_time
from article t
where 1=1
    and t.** like '%%'


like关键字加上union,最后你按照时间排序即可,前提是你三张表的结构要统一一下,不然union不了

【热门文章】
【热门文章】