订购sql选择结果

时间:2018-07-08 15:19:58

标签: php sql

我正在通过以下命令从sql中选择数据:

该命令可以包含多个单词,例如

   select * from table where title like '%deadpool%' ////1

   select * from table where title like '%deadpool%' && like '%world%' ////2

所以显示的结果是:

***************** 1 *************

  1. 新的死侍在这里
  2. 死侍

***************** 2 *************

  1. .deadpool.world。
  2. 死侍世界

现在,我要按以下顺序订购列表:

***************** 1 *************

  1. 死侍
  2. 新的死侍在这里

***************** 2 *************

  1. 死侍世界
  2. cdsghjvjdhgdeadpoolvworlddsbvs或其他

我想以某种方式对它进行排序,以便如果结果中出现搜索词的整个单词,则应该首先显示它。

1 个答案:

答案 0 :(得分:0)

我怀疑您正在寻找:

select t.*
from table t
where title like '%deadpool%' and
      title like '%world%'
order by ((title like '%deadpool%') + (title like '%world%')) desc;

但是,您很有可能确实希望按相关性顺序进行全文搜索。您可以检查documentation来确定这是否是您真正想要的。