Please help me to sort my table in MySQL. I need to make it only once, not for use in scripts or somewhere else dynamically.
Here’s my table ‘page’:
| ... | title | ... | parent_id | ... | position | ... |
I need to update table with the rule: select all rows with the same parent_id, sort them by title DESC and set first row position = 0, second = 1 etc. And loop this for all parent_id.
I made a query to display what I need, but with no UPDATE and only for one parent_id. But as there are some thousands parent_id this query needs a loop. And after selecting, position must be updated to have the same value as @number for current row.
SET @number = -1;
SELECT @number:=@number+1 AS number, p.* FROM `page` AS p WHERE parent_id=1
ORDER BY title DESC;
Thanks for your help!
If your (parent_id,title) pairs aren’t unique use your primary key as join condition – you will need to add it to select in parenthesis.