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 8565233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:22:45+00:00 2026-06-11T17:22:45+00:00

I have a table with two columns. +——+——+ | data | num | +——+——+

  • 0

I have a table with two columns.

+------+------+
| data | num  | 
+------+------+
| a    |      | 
| a    |      |
| a    |      |
| b    |      |
| b    |      |
| c    |      |
| d    |      |
| a    |      |
| b    |      | 
+------+------+

I want the column "num" displays an incremental counter for each duplicate entry:

+------+------+
| data | num  | 
+------+------+
| a    |    1 | 
| a    |    2 |
| a    |    3 |
| b    |    1 |
| b    |    2 |
| c    |    1 |
| d    |    1 |
| a    |    4 |
| b    |    3 | 
+------+------+

Is this possible to be done without any other scripting besides a mySQL query?

UPDATE:

extended question here

  • 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-06-11T17:22:46+00:00Added an answer on June 11, 2026 at 5:22 pm

    Unfortunately, MySQL does not have windowing functions which is what you will need. So you will have to use something like this:

    Final Query

    select data, group_row_number, overall_row_num
    from
    (
      select data,
            @num := if(@data = `data`, @num + 1, 1) as group_row_number,
            @data := `data` as dummy, overall_row_num
      from
      (
        select data, @rn:=@rn+1 overall_row_num
        from yourtable, (SELECT @rn:=0) r
      ) x
      order by data, overall_row_num
    ) x
    order by overall_row_num
    

    see SQL Fiddle with Demo

    Explanation:

    First, inner select, this applies a mock row_number to all of the records in your table (See SQL Fiddle with Demo):

    select data, @rn:=@rn+1 overall_row_num
    from yourtable, (SELECT @rn:=0) r
    

    Second part of the query, compares each row in your table to the next one to see if it has the same value, if it doesn’t then start the group_row_number over (see SQL Fiddle with Demo):

    select data,
          @num := if(@data = `data`, @num + 1, 1) as group_row_number,
          @data := `data` as dummy, overall_row_num
    from
    (
      select data, @rn:=@rn+1 overall_row_num
      from yourtable, (SELECT @rn:=0) r
    ) x
    order by data, overall_row_num
    

    The last select, returns the values you want and places them back in the order you requested:

    select data, group_row_number, overall_row_num
    from
    (
      select data,
            @num := if(@data = `data`, @num + 1, 1) as group_row_number,
            @data := `data` as dummy, overall_row_num
      from
      (
        select data, @rn:=@rn+1 overall_row_num
        from yourtable, (SELECT @rn:=0) r
      ) x
      order by data, overall_row_num
    ) x
    order by overall_row_num
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table which has two columns(CAGE,HCC) nulls allowed. I want to display duplicate
I have a table with two columns. I want to center the right-most column
I have a table with two columns: column A column B 1 2 1
I have a table with two columns: latitude and longitude. I want to get
I have a table containing two columns in SQL that I want to extract
I have a table with two columns. Items in the first column represent one
I have simple table with two columns: id INTEGER as a key, and data
Say I have a table with two columns called A, and B. I want
Say I have table with two columns: Name, Age. I want to design a
I have a table with two columns, date and score. I want to find

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.