表
id Company Name Price
1 xxx namea 1
2 xxx nameh 1.1
3 xxx nameg 2
4 yyy namef 1.5
5 zzz named 1.7
6 zzz names 0.9
我需要这样的结果
dCounter Company Name Price
1 xxx namea 1
1 xxx nameh 1.1
1 xxx nameg 2
2 yyy namef 1.5
3 zzz named 1.7
3 zzz names 0.9
正如您所看到的,我们计算公司的数量,它应该适用于分页。这意味着如果我们在每秒页面上有x2条目,那么将会有dCounter 1和2,在第三页上只有3个。
问题出在dCounter中,因为它应该再次计入每个页面。由公司ASC订购,名称为ASC,价格为ASC"。
我该如何为此创建查询?
答案 0 :(得分:0)
按公司计算排名()。
select
@rn := case when @company <> company then @rn + 1 else @rn end as rn,
@company := company as cia,
Name,
Price
from
(select @rn := 0) i,
(select @company := company as company, Name, Price
from ff order by company) t;
结果:
+----+-----+-------+-------+
| rn | cia | Name | Price |
+----+-----+-------+-------+
| 1 | xxx | namea | 1,00 |
+----+-----+-------+-------+
| 1 | xxx | nameh | 1,10 |
+----+-----+-------+-------+
| 1 | xxx | nameg | 2,00 |
+----+-----+-------+-------+
| 2 | yyy | namef | 1,50 |
+----+-----+-------+-------+
| 3 | zzz | named | 1,70 |
+----+-----+-------+-------+
| 3 | zzz | names | 0,90 |
+----+-----+-------+-------+
可在此处查看:http://rextester.com/ACY4401