我的桌子或多或少看起来像这样:
**Day** | **Mileage**
----------------
1 | 13
2 | 2
3 | 25
4 | 15
5 | 20
6 | 8
7 | 17
8 | 12
9 | 16
10 | 5
如何编写SQL查询:
答案 0 :(得分:2)
总结:
select mday, (select SUM(miles) from mm s where s.mday <= mm.mday) tot_miles
from mm;
每天最多5个里程
select mday, miles from mm order by miles desc limit 5
更改列名和表名以匹配您的。
由于您添加了&#34;没有使用限制&#34;,这是奇怪的,您可以试试这个:
set @r =0;
select * from (
select *, @r :=@r+1 as r from mm order by miles desc) e
where r<=5
答案 1 :(得分:1)
因为你不想在这里使用限制是一种繁琐的方式
1)
select sum(mileage) from xyz where day between 2 and 15;
2)
mysql> select @rownum:=@rownum+1 'rank',mileage
from (select mileage from xyzorder by mileage desc)mil,(select @rownum:=0)r
where @rownum<5;