表Table中的结构是 ID title body url. 要求用一个sql查询将url匹配的排在最前,title匹配的其次,body匹配最后,没有任何字段匹配的,不返回。
思路:在表中每一字段对关键字进行联合查询,通过创建一个临时数值字段进行排序,达到匹配顺序的效果。
Query:
select t.[id],t.mark from
(
select [Table].[id], 3 as mark from [Table] where [Table].[url] like '%2astudio%'
union
select [Table].[id], 2 as mark from [Table] where [Table].[title] like '%2astudio%'
union
select [Table].[id], 1 as mark from [Table] where [Table].[body] like '%2astudio%'
) as t order by mark desc