Mysql 多表搜索

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

阅读 4.2k
3 个回答

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

上面被采纳的回答说的结构统一应该不是说表的结构统一 是结果字段统一
例如:
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 '%%'

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题