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
Unfortunately, MySQL does not have windowing functions which is what you will need. So you will have to use something like this:
Final Query
see SQL Fiddle with Demo
Explanation:
First, inner select, this applies a mock
row_numberto all of the records in your table (See SQL Fiddle with Demo):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_numberover (see SQL Fiddle with Demo):The last select, returns the values you want and places them back in the order you requested: