简体   繁体   English

在相关排序的sql中提升特色结果

[英]boost featured results in relevance sorted sql

I have a DB table of jobs. 我有作业的数据库表。 The table has a column 'featured' = NULL or 1. 该表具有一列“功能” = NULL或1。

A user can search the table using keywords. 用户可以使用关键字搜索表。 And I sort them by relevancy, date, or salary, like below (relevancy): 然后按相关性,日期或薪水对它们进行排序,如下所示(相关性):

SELECT SQL_CALC_FOUND_ROWS *, MATCH(`title`) 
            AGAINST ("*'.$keywords.'*") 
            AS Relevance 
            FROM jobs2 
            WHERE MATCH(`title`) 
            AGAINST ("*'.$keywords.'*") 
            ORDER BY Relevance
            LIMIT 0,50

I want any jobs that are 'featured' to be first on each page (but each featured job still in order themselves). 我希望所有“特色”工作都在每个页面的第一位(但每个特色工作仍要按顺序排列)。

eg if a natural search was to return (in order): 例如,如果自然搜索要返回(按顺序):

Job1  not featured
Job2  not featured
Job3  featured
Job4  not featured
Job5  featured

I want featured jobs at the top in order like: 我想要按顺序排列顶部的特色职位,例如:

Job3  featured
Job5  featured
Job1  not featured
Job2  not featured
Job4  not featured

How can I achieve this? 我该如何实现? Ideally in the SQL query itself if possible 如果可能的话,最好是在SQL查询本身中

Well, if you simply want all of the featured ones first of all the listings, you can just sort by two columns: 好吧,如果您只是想要所有清单中的所有特色商品,则可以按两列进行排序:

SELECT SQL_CALC_FOUND_ROWS *, MATCH(`title`) 
  AGAINST ("*'.$keywords.'*") 
  AS Relevance 
  FROM jobs2 
  WHERE MATCH(`title`) 
  AGAINST ("*'.$keywords.'*") 
  ORDER BY featured desc, Relevance desc -- <-- This here
  LIMIT 0,50

If instead you want to have the featured listings first on each page but not to impact the overall sorting otherwise, you can do it either in your application logic, or use the same query as a subquery and reorder its results: 相反,如果您想在每个页面上首先显示特色列表,但又不影响整体排序,则可以在您的应用程序逻辑中进行操作,或者使用与子查询相同的查询并对结果进行重新排序:

SELECT * from 
  (SELECT SQL_CALC_FOUND_ROWS *, MATCH(`title`) 
    AGAINST ("*'.$keywords.'*") 
    AS Relevance 
    FROM jobs2 
    WHERE MATCH(`title`) 
    AGAINST ("*'.$keywords.'*") 
    ORDER BY Relevance desc
    LIMIT 0,50) AS X
ORDER BY X.featured desc, X.Relevance desc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM