简体   繁体   English

在SQL表中选择两个随机行,其中列的值非常接近

[英]SELECTing two random rows in an SQL table where the values of a column come very close

I ran into a little problem. 我遇到了一个小问题。 I am creating a web app with code snippets. 我正在创建带有代码片段的Web应用程序。 Users are confronted with two random code snippets which are tagged similarly and they must choose which of the two they like the most, or they can skip (similar to Facemash, but with code snippets instead of pictures). 用户面临着两个随机的代码片段,它们被相似地标记,他们必须选择最喜欢的两个代码片段,或者他们可以跳过(类似于Facemash,但是使用代码片段而不是图片)。

I have table called CodeSnippets with these fields: 我有名为CodeSnippets的表,其中包含以下字段:

  • Id — ID of the snippet ID-代码段的ID
  • Score — Score of the snippet 分数—代码段的分数
  • Some more non-important columns 一些不重要的列

Score is a real number. 分数是实数。

My question: how can I write a query that gets two random records with scores that are very close? 我的问题:如何编写查询以获取两条随机记录,且它们的得分非常接近? The problem is that I have to order trice: RAND(), Score and RAND() with a margin. 问题是我必须订购骰子:RAND(),Score和RAND()并带有边距。

This is what I already have: 这就是我已经拥有的:

SELECT Id, Score, Code FROM CodeSnippets ORDER BY RAND() LIMIT 2

But this simply picks two random records, even if their scores lie far away from eachother :) 但这只是选择了两个随机记录,即使它们的得分彼此之间距离很远:)

Could anyone point me in the right direction? 有人能指出我正确的方向吗?

Can you do it with a subquery? 可以使用子查询吗? First have a self-join query to select all pairs with score within a threshold, then take the first random pair: 首先进行自联接查询,以选择分数在阈值内的所有对,然后选择第一个随机对:

SELECT TOP 1 Id1, Id2
FROM (
  SELECT C1.Id AS Id1, C2.Id AS Id2 FROM CodeSnippets C1, CodeSnippet C2
  WHERE C1.Id <> C2.Id AND ABS(C1.Score-C2.Score) < [Threshold]
)
ORDER BY RAND()

Be careful because not all database engines support sub-queries (although most should). 请注意,因为并非所有数据库引擎都支持子查询(尽管大多数都应支持)。 Most engines will also optimize/rewrite sub-queries to run fast, but some embedded database engines may just run the sub-query, create a temp table in memory, then run the outer query over the temp table, resulting in poor performance. 大多数引擎还将优化/重写子查询以使其快速运行,但是某些嵌入式数据库引擎可能仅运行子查询,在内存中创建一个临时表,然后在该临时表上运行外部查询,从而导致性能下降。 If that is your case, you'll need to rewrite this query using joins. 如果是这种情况,则需要使用联接重写此查询。

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

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