Skip to content
Advertisement

How to make a column describing order of elements?

I want to add a column to a table that describes the order of the rows by other value. For example, i have the following data:

I want a query to get the following:

The rules that apply here is: – On same popularity, order by id – The highest popularity is the first on the new order – The null popularity is the lowest on the new order

Im trying to do this because the table has millions of rows, and doing a pagination by popularity and using offset is really slow. So having this new order will allow me to do something like id > page*page_amount.

It would be great to have an update query that sets this value because the select query with offset is really slow.

Thanks

Advertisement

Answer

I found the solution for this if anyone needs the same:

It uses a dynamic variable that changes in every step.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement