表格中的数据将每天添加一个抓取工具。所以第一个条目应该每天排名第一。但是因为爬虫可以在2倍的时间内爬行产品。显示下一行具有相同的名称。如果这是真的那么它需要与前一行具有相同的排名。
这是我到目前为止所做的事情:
SELECT a.product_ranking_id, a.Productinfo, a.Date,
(
SELECT index(Productinfo)
From ProductRanking as b
Where a.Date = b.Date
) as Rank
From ProductRanking as a
结束结果
+------------------------+--------------+------------+------+
| Table: ProductRanking | | | |
+------------------------+--------------+------------+------+
| product_ranking_id | Productinfo | Date | Rank |
+------------------------+--------------+------------+------+
| | product 1 | 01-01-2018 | 1 |
+------------------------+--------------+------------+------+
| | product 1 | 01-01-2018 | 1 |
+------------------------+--------------+------------+------+
| | product 2 | 01-01-2018 | 2 |
+------------------------+--------------+------------+------+
| | product 3 | 01-01-2018 | 3 |
+------------------------+--------------+------------+------+
| | product 445 | 01-01-2018 | 4 |
+------------------------+--------------+------------+------+
| | product 3 | 02-01-2018 | 1 |
+------------------------+--------------+------------+------+
| | product 89 | 02-01-2018 | 2 |
+------------------------+--------------+------------+------+
| | product 89 | 02-01-2018 | 2 |
+------------------------+--------------+------------+------+
解决方案:
在Gordon Linoff的帮助下,我找到了一个解决方案。这就是为什么我标记他的答案。 如果它可以更好,总是欢迎..
SELECT *,(@i := if(a.Productinfo != b.Productinfo,
if(a.date = b.date, @i + 1,@i := 1), @i) ) as Rank
From
productrank as a
left join productrank as b on b.product_ranking_id = a.product_ranking_id -1
Cross join (SELECT @i := 1) params
;
答案 0 :(得分:0)
我想你想要:
select pr.*,
(@rn := if(@pd = concat_ws(' ', productinfo, date), @rn + 1,
if(@pd := concat_ws(' ', productinfo, date), 1, 1)
)
) as rank
from (select pr.*
from ProductRanking pr
order by Productinfo, Date
) pr cross join
(select @pd := '', @rn := 0) params;
在旧版本的MySQL中,子查询不是必需的。您可以在外部查询中order by
。
编辑:
假设product_ranking_id
包含顺序ID:
select pr.*,
(@rn := if(@p = productinfo, @rn + 1,
if(@p := productinfo, 1, 1)
)
) as rank
from (select pr.*
from ProductRanking pr
order by Productinfo, product_ranking_id
) pr cross join
(select @p := '', @rn := 0) params;