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

  • Home
  • SEARCH
  • 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 6619949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:02:13+00:00 2026-05-25T21:02:13+00:00

If I make a selection from my MySQL table like: SELECT * FROM mytable

  • 0

If I make a selection from my MySQL table like:

SELECT * FROM mytable ORDER BY country_id, category

I get the following table:

+------------+----------+-------+
| country_id | category | order |
+------------+----------+-------+
|          1 |        A |     0 |
|          1 |        B |     0 |
|          1 |        F |     0 |
|          3 |        A |     0 |
|          3 |        C |     0 |
|          5 |        B |     0 |
|          5 |        L |     0 |
|          5 |        P |     0 |
+------------+----------+-------+

What I would like to do is update the order column so that the value of that column is the order of that row for it’s country_id. In other words the final table should look like this:

+------------+----------+-------+
| country_id | category | order |
+------------+----------+-------+
|          1 |        A |     1 |
|          1 |        B |     2 |
|          1 |        F |     3 |
|          3 |        A |     1 |
|          3 |        C |     2 |
|          5 |        B |     1 |
|          5 |        L |     2 |
|          5 |        P |     3 |
+------------+----------+-------+

If I use the original query given at the top in a subquery, that would give me the correct order but I can’t figure out how to iterate through the table and suspect I thinking about it wrongly.

How can I get this result?

Many thanks

  • 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-25T21:02:14+00:00Added an answer on May 25, 2026 at 9:02 pm

    Try to use this query –

    UPDATE mytable t1
      JOIN (
        SELECT t1.country_id, t1.category, COUNT(*) `order`
        FROM mytable t1
        LEFT JOIN mytable t2
          ON t2.country_id = t1.country_id AND t2.category <= t1.category
        GROUP BY
          t1.country_id, t1.category
      ) t2
      ON t1.country_id = t2.country_id AND t1.category = t2.category
    SET t1.`order` = t2.`order`
    

    As an aside note, you can remove order field from the table because this can calculated this on the fly.

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

Sidebar

Related Questions

Is this kind of mysql query possible? SELECT power FROM .$table. WHERE category IN
i want to make a mysql selection but i need to select two types
I am currently trying to make a sectioned table like this: Section 1: Entry
In a MySQL database I have a table with the following primary key PRIMARY
I would like to clarify how browser scripting (such as javascript/jquery) differs from server
I want to show a random record from the database. I would like to
Ok, right now I trying to get a list to echo out from mysql
Using mysql (and php) I try to hide out of a select menu category's
I am building a CMS for editing page content. I would like to make
I need select result from mysql regarding query string. Let's string will be: Z

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.