I'm trying to figure out what this query exactly performs. In particular the part in which are used the variables @
and the assignment :=
.
The first part is quite simple because we have a nested query from derived table t1 but what is not really clear to me is the result of the column rn. Here's the query:
SELECT
t1.user_id,
t1.percentage,
t1.id,
t1.name,
(@rn := if(@uid = t1.user_id, @rn + 1,
if(@uid := t1.user_id, 1, 1))
) as rn
FROM
(SELECT
pbt.user_id,
pbt.percentage,
t.id, t.name
FROM
user_purchased_brand_tags AS pbt
JOIN tags t on t.id = pbt.tag_id
ORDER BY pbt.user_id, pbt.percentage desc) t1