简体   繁体   English

MySQL选择总和大于阈值的记录

[英]MySQL select records with sum greater than threshold

I need to select records based on file size listed in a MySQL database table, with a single query (no store procedures). 我需要根据MySQL数据库表中列出的文件大小来选择记录,并使用单个查询(无存储过程)。 The record set should contain all records where the sum total file size equals or if needed exceeds a specific threshold. 记录集应包含文件总和等于或必要时超过特定阈值的所有记录。 (Example, threshold = 30, results return 3 records with file sizes equal to 10, 10, 20 or 10, 10, 10 or one record with a file size of 32) (例如,阈值= 30,结果返回3条记录,文件大小等于10、10、20或10、10、10或一条记录,文件大小为32)

table

+----+---------+-----------+
| id | user_id | fileSize  |
+----+---------+-----------+
|  1 |       1 |      9319 |
|  2 |       1 |     51683 |
|  3 |       1 |     19776 |
|  4 |       1 |    395890 |
|  5 |       1 |      7132 |
|  6 |       1 |     97656 |
|  7 |       1 |      9798 |
|  9 |       1 |     16096 |
| 10 |       1 |    113910 |
| 11 |       1 |    160037 |
+----+---------+-----------+

After researching and trying a number of different solutions the best query that I have come up with looks like this: 在研究并尝试了许多不同的解决方案之后,我提出的最佳查询如下所示:

SELECT f1.user_id, f1.id AS file_id, f1.fileSize, SUM(f2.fileSize) AS totalSum
FROM files AS f1 
INNER JOIN files AS f2 ON f1.id >= f2.id 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING totalSum <= 350000;

Example results 结果示例

+---------+---------+-----------+----------+
| user_id | file_id |  fileSize | totalSum |
+---------+---------+-----------+----------+
|       1 |       1 |      9319 |     9319 |
|       1 |       2 |     51683 |    61002 |
|       1 |       3 |     19776 |    80778 |
+---------+---------+-----------+----------+

Desired results 所需结果

+---------+---------+-----------+----------+
| user_id | file_id |  fileSize | totalSum |
+---------+---------+-----------+----------+
|       1 |       1 |      9319 |     9319 |
|       1 |       2 |     51683 |    61002 |
|       1 |       3 |     19776 |    80778 |
|       1 |       4 |    395890 |   476668 |
+---------+---------+-----------+----------+

Or 要么

+---------+---------+-----------+----------+
| user_id | file_id |  fileSize | totalSum |
+---------+---------+-----------+----------+
|       1 |       3 |    395890 |   395890 |
+---------+---------+-----------+----------+

What isn't working with the query above is that the threshold will never be met, as it is based on HAVING lesser than the threshold (greater than just returns crazy amounts of records well above the threshold). 上面的查询无法正常工作的是,将永远不会满足阈值,因为它基于HAVING小于阈值(大于仅返回疯狂地超过阈值的疯狂记录)。 Also, if there are any records in the set that have a file size exceeding the threshold, the query result sometimes returns empty. 此外,如果集合中有任何记录的文件大小超过阈值,则查询结果有时会返回空。 Ideal results would meet or slightly exceed the threshold and may contain many records or a single record if the single file size matched or exceeded the threshold. 理想结果将达到或稍微超过阈值,并且如果单个文件大小匹配或超过阈值,则可能包含许多记录或单个记录。

Any help would be appreciated. 任何帮助,将不胜感激。 I think this is the first time I have posted a question online in about five years. 我认为这是五年来我第一次在网上发布问题。 Seriously, been stuck on this for a week. 认真地,坚持了一个星期。 ~ Thx 〜谢谢

This seems to be fitted for a UNION resultset. 这似乎适合于UNION结果集。 So you have to get 2 queries (one for each "criteria") and join their result using union. 因此,您必须获得2个查询(每个“条件”一个),并使用并集加入其结果。

First query would become: 第一个查询将变为:

SELECT f1.user_id, f1.id AS file_id, SUM(f1.fileSize) AS totalSum
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING totalSum <= 350000;

Now you need the query that select if size is too big: 现在,您需要选择是否太大的查询:

SELECT f1.user_id, f1.id AS file_id, MAX(f1.fileSize) AS max
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING max >= 350000;

Next you want to combine them in a single query. 接下来,您要将它们合并在一个查询中。 Since both have the same fields you can simply "union" the result 由于两者具有相同的字段,因此您可以简单地“合并”结果

SELECT f1.user_id, f1.id AS file_id, SUM(f1.fileSize) AS totalSum
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING totalSum <= 350000
UNION
SELECT f1.user_id, f1.id AS file_id, MAX(f1.fileSize) AS max
FROM files AS f1 
WHERE f1.user_id = 1
GROUP BY f1.id 
HAVING max >= 350000;

PS: You had "ON f1.id >= f2.id" as join criteria, not sure why the > that would be very case-specific :) PS:您已将“ ON f1.id> = f2.id”作为连接标准,不确定为什么>会非常区分大小写:)

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

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