Order by Maximum condition match

前端 未结 4 649
名媛妹妹
名媛妹妹 2020-12-16 22:09

Please help me to create a select query which contains 10 \'where\' clause and the order should be like that: the results should be displayed in order of most keywords(where

4条回答
  •  不知归路
    2020-12-16 22:53

    EDIT: This answer was posted before the question was modified with a concrete example. Marcelo's solution addresses the actual problem. On the other hand, my answer was giving priority to matches of specific fields.


    You may want to try something like the following, using the same expressions in the ORDER BY clause as in your WHERE clause:

    SELECT    *
    FROM      your_table
    WHERE     field_1 = 100 OR
              field_2 = 200 OR
              field_3 = 300
    ORDER BY  field_1 = 100 DESC,
              field_2 = 200 DESC,
              field_3 = 300 DESC;
    

    I've recently answered a similar question on Stack Overflow which you might be interested in checking out:

    • Is there a SQL technique for ordering by matching multiple criteria?

提交回复
热议问题