簡體   English   中英

SQL:從數據庫中選擇最多一個數字(大小)

[英]SQL: Select from Database up to a number (size)

我想選擇不超過一定數量(大小)的行。 我的SQL( SQL Fiddle ):

id  user_id     storage
1   1           1983349
2   1           42552
3   1           367225
4   1           1357899
37  1           9314493

我只想選擇不超過一定數量(大小)的所有行。 在這里像這樣:

Select * from uploads where storage < 410000

它應該在這里得到這樣的東西:

id  user_id     storage
2   1           42552
3   1           367225

ID'2'和'3'的摘要為409777。

您需要一些獲取累計金額的方法。 在MySQL中,最簡單的方法是使用變量:

select u.*
from (select u.*, (@s := @s + storage) as cume_storage
      from uploads u cross join (select @s := 0) params
      order by id
     ) u
where cume_storage < 410000;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM