Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:09:51+00:00 2026-05-29T07:09:51+00:00

Please help me to sort my table in MySQL. I need to make it

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:09:51+00:00Added an answer on May 29, 2026 at 7:09 am
    SELECT @p:=0, @parent:=0;
    UPDATE page p1
    JOIN
    ( SELECT title, 
      CASE WHEN @parent<>parent_id THEN @p:=0 ELSE @p:=@p+1 END as position,
      @parent:=parent_id as parent_id
      FROM page
      ORDER BY parent_id, title DESC ) p2
    ON p1.title = p2.title AND p1.parent_id = p2.parent_id
    SET p1.position = p2.position
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My English is not good, sorry... I use tablesorter (tablesorter.com) to sort table in
Please help to the newbie in WPF! I need to build a TreeView with
Please help me with a query. Assume that we have a table with columns:
I need some hibernate/SQL help, please. I'm trying to generate a report against an
Looking for some help and advice please from Super Guru MySQL/PHP pros who can
I'd appreciate your help on this please : I've got a table which logs
Please help with this issue, I'm trying to populate my table cells with coreData
Can you please check this mysql query and maybe help with what's wrong with
Please help! I'm really at my wits' end. My program is a little personal
Please help! Background info I have a WPF application which accesses a SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.