mysql多个查询结果组合
问题描述:
比如我有一个article表,前端搜索文章内容是给参数search,我想要的是先查询article的title 是否有匹配字段并按照时间排序,然后再查询article的contents是否匹配字段,并按照时间排序,然后再把这连个查询结果整合返回给前端,该怎么设计查询语句啊?
答
select * from article where title like '%"+search+"%' order by time
union
select * from article where contents like '%"+search+"%' order by time
答
如果前端给你两个参数,你想要的最后结果是想要article表的title和contents字段里的内容都要能和前端给的参数匹配的话,应该不用将两个结果整合一条sql应该就满足了
select * from article where title like '%参数1%' and contents like '%参数2%' order by 时间那一列 ;