简体   繁体   English

在最佳匹配上排序SQL查询

[英]Order SQL query on best match

I get a list of results returned from my query that I want to order on the best match. 我得到了一个从我的查询返回的结果列表,我想在最佳匹配上订购。 I will try to be as clear as I can be, but if something is not clear enough, let me know and I will try and make it more clear. 我会尽量保持清醒,但如果事情不够清楚,请告诉我,我会尽力使其更清楚。

A user has already entered a list of settings, called findsettings . 用户已经输入了一个名为findsettings的设置列表。 With these findsettings I am searching for products. 通过这些findsettings我正在寻找产品。 This all goes well, until he should go sort out the best match. 这一切顺利,直到他应该找出最佳匹配。

There are several fields such as min_review, max_price, allows_pets, etc . 有几个字段,如min_review, max_price, allows_pets, etc

I want to order on. 我想订购。 For example he needs to order first on products with a min_review of 40% , then max_price = 10.00 and then allows_pets = 0 . 例如,他需要首先订购min_review of 40% ,然后是max_price = 10.00然后allows_pets = 0 You can do that with an OR , but I want the results that don't quiet match also to show, only at the bottom of the list. 您可以使用OR执行此操作,但我希望也不会显示不安静匹配的结果,仅显示在列表的底部。 So basically he should show the best match first, then the 2nd one, the 3rd one etc., until the products that matches the least. 所以基本上他应该首先显示最佳匹配,然后是第二个,第三个等,直到匹配最少的产品。

I'm not sure how to handle this, so I hope you can help me sort that out. 我不知道如何处理这个问题,所以我希望你能帮助我解决这个问题。

The basic principle is to create a relevance score for each record and then sort by that. 基本原则是为每条记录创建相关性分数,然后按此排序。

Where you want each criterion to have equal weight, in MySQL you can just add each boolean expression together (note that this is non-standard SQL—in other RDBMS you may have to use CASE to test the condition and yield a number): 如果你希望每个标准具有相同的权重,在MySQL中你可以将每个布尔表达式加在一起(注意这是非标准的SQL-在其他RDBMS中你可能必须使用CASE来测试条件并产生一个数字):

ORDER BY (review>=40/100) + (price<=10.00) + (allows_pets=0) DESC

If the criteria are not equally weighted, you can either multiply each expression by its weight: 如果标准的权重不相等,您可以将每个表达式乘以其权重:

ORDER BY 5*(review>=40/100) + 3*(price<=10.00) + 1*(allows_pets=0) DESC

And/or if those that match on some subset should always appear first irrespective of the later results, you can divide your ORDER BY clause: 和/或如果在某些子集上匹配的那些应该始终首先出现而不考虑后面的结果,您可以划分ORDER BY子句:

ORDER BY 5*(review>=40/100) + 3*(price<=10.00) DESC,
         1*(allows_pets=0) DESC

Should you want to see the relevance score in your results, you can similarly add any of the above expressions to your SELECT (and then use the resulting column in your ORDER BY clause so as to avoid writing it twice): 如果要在结果中查看相关性分数,可以类似地将以上任何表达式添加到SELECT (然后在ORDER BY子句中使用结果列,以避免将其写入两次):

SELECT   *,
         (review>=40/100) + (price<=10.00) + (allows_pets=0) AS relevance,
FROM     my_table
ORDER BY relevance DESC

Note that should you want to obtain records which have a value closest to some target (eg min_review close to 40%, rather than exact), you can take the (absolute?) difference between it and your target value: 请注意,如果您想获取具有最接近某个目标的值的记录(例如, min_review接近40%,而不是精确),您可以获取它与目标值之间的(绝对?)差异:

IF(review>=40/100, review-40/100, NULL)

However, you must be careful to weight your expressions appropriately if/when combining with others in a single criterion. 但是,如果/在与单个标准中的其他人合并时,您必须小心对表达进行适当加权。

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

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