简体   繁体   English

计算行,直到达到值

[英]Count rows until value is reached

I am trying to find a way to count the number of users until the number is reached. 我试图找到一种方法来计算用户数量,直到达到该数量为止。 Here's somewhat of how my table is setup. 这是我的桌子的设置方式。

ID    Quantity
1           10
2           30
3           20
4           28

Basically, I want to organize the row quantity to be in order from greatest to least. 基本上,我要按最大到最小的顺序组织行数。 Then I want it to count how many rows it takes from going from the highest quantity to whatever ID you supply it with. 然后,我希望它计算从最大数量到提供的ID所需的行数。 So for example, If I was looking for the ID #4, It would look through the quantity from from greatest to least, then tell me that it is row #2 because it took only 2 rows to reach it since it contains the 2nd highest quantity. 因此,例如,如果我正在查找ID#4,它将从最大到最小浏览该数量,然后告诉我它是#2行,因为它只用了2行,因为它包含第二高的行数量。

There is another way I can code this, but I feel it is too demanding of a resource and involves PHP. 我可以用另一种方式对此进行编码,但是我觉得它对资源的要求太高,涉及PHP。 I can do a loop on my database based on the greatest to least, and every time it goes through another loop, I add +1. 我可以对数据库进行从最大到最小的循环,每次经过另一个循环时,我都会加+1。 So, that way, I could do an IF statement to determine when it reaches my value. 这样,我可以执行IF语句来确定何时达到我的价值。 However, when I have thousands of values it would have to go through, I feel like that would be too resource demanding. 但是,当我拥有成千上万的价值时,我将不得不经历这一切,这对资源的要求太高了。

Overall, this is a simple sort problem. 总的来说,这是一个简单的排序问题。 Any data structure can give you the row of an item, with minor modifications in some cases. 任何数据结构都可以为您提供项目的行,在某些情况下,只需稍作修改即可。

If you are planning on using this operation multiple times, it is possible to beat the theoretical O(n log(n)) running time with an amortized O(log(n)) by maintaining a separate sorted copy of your table sorted by quantity. 如果您计划多次使用此操作,则可以通过维护按数量排序的表的单独排序副本,以摊销O(log(n))击败理论O(n log(n))运行时间。 。 This reduces the problem to a binary search. 这样可以将问题简化为二进制搜索。

A third alternative is to maintain a virtual linked list of table entries in the new sort order. 第三种选择是以新的排序顺序维护表条​​目的虚拟链接列表。 This would increase the insert times into the table to O(n), but would reduce this problem to O(1) 这会将表中的插入时间增加到O(n),但是将此问题减少到O(1)

A fourth solution would be to maintain a virtual balanced tree, however, despite the good theoretical performance, this solution is likely to be extremely hard to implement. 第四个解决方案是维护虚拟平衡树,但是,尽管具有良好的理论性能,但该解决方案可能极难实现。

It might not be the answer you are expecting but: you can't "stop" the execution of a query after you reach a certain value. 它可能不是您期望的答案,但是:达到某个值后,您将无法“停止”查询的执行。 MySQL always generate the full result set before you can analyse it. MySQL始终会生成完整的结果集,然后才能对其进行分析。 This is because, it order to sort the results by Quantity , MySQL needs to have all the rows. 这是因为,为了按Quantity对结果进行排序,MySQL需要具有所有行。

So if you want to do this is pure MySQL, you need to count the row numbers (as explained here MySQL - Get row number on select ) in a temporary table and then select your ID from there. 因此,如果要使用纯MySQL,则需要在临时表中计算行号(如此处的MySQL-在select上获取行号 ),然后从那里选择ID。

Example: 例:

SET @rank = 0;
SELECT *
FROM (
  SELECT Id, Quantity, @rank := @rank + 1 as rank
  FROM table
  ORDER BY Quantity
) as ordered_table
WHERE Id = 4;

If performance is an issue, you could probably speed this up a bit with an index on Quantity (to be tested). 如果性能是一个问题,则可以通过“ Quantity (待测试)的索引来加快速度。 Otherwise the best way is to store the "rank" value in a separate table (containing only 2 columns: Id and Rank), possibly with a trigger to refresh the table on insert/update. 否则,最好的方法是将“等级”值存储在单独的表(仅包含2列:Id和Rank)中,并可能带有触发器以在插入/更新时刷新表。

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

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