基于包含搜索值的列来排序查询

时间:2016-04-22 15:48:13

标签: mysql

所以我有一个叫做产品的表

产品:标题,标签,帖子,约,约2

当有人在产品中搜索xxx时,我希望xxx列中包含tags的产品位于posttitle之后aboutabout2

但是当我运行此查询时

    select id , title , tags , about , about2 , post from products where 

   `title` LIKE '%xxx%' ESCAPE '!' OR
   `tags` LIKE '%xxx%' ESCAPE '!' OR
   `post` LIKE '%xxx%' ESCAPE '!' OR
   `about` LIKE '%xxx%' ESCAPE '!' OR
   `about2` LIKE '%xxx%' ESCAPE '!' 

   ORDER BY FIELD( tags, post, title , about , about2) 

这是导出的结果(每行的标题都设置为包含xxx的列的名称)

(`id`, `title`, `tags`, `about`, `about2`, `post`) 
VALUES
(459, 'TAGS', 'xxx\n', '', '', ''),
(455, 'ABOUT 2', '', '', ' xxx', ''),
(456, 'ABOUT', '', ' xxx\n', '', ''),
(457, 'TITLE - xxx\n', '', '', '', ''),
(458, 'POST', '', '', '', 'xxx\n')

你可以看到ABOUT位于顶部,标题,帖子位于底部

我如何对查询进行排序以获得顶部标记,其余如上所述?

我试过

ORDER BY (tags LIKE '%xxx%') ,
(post LIKE '%xxx%') ,
(title LIKE '%xxx%') ,
(about LIKE '%xxx%') ,
(about2 LIKE '%xxx%') 

我仍然可以在顶部找到专栏!

0 个答案:

没有答案