From the sake of this question, I will call the source table aaronlax
PROPOSED SOLUTION
SET @ndx = 0;
SET @cur_hash = '';
SELECT entry_id,CONCAT(url_title,IF(ndx>0,CONCAT('-',ndx),'')) url_title
FROM (SELECT
@new_hash := MD5(url_title),
(@ndx := IF(@new_hash=@cur_hash,@ndx+1,0)) ndx,
@cur_hash := MD5(url_title),
url_title,entry_id
FROM (SELECT url_title,entry_id FROM aaronlax ORDER BY url_title) AA) A;
SAMPLE DATA
use test
DROP TABLE IF EXISTS aaronlax;
CREATE TABLE aaronlax
(
entry_id int(10) unsigned NOT NULL AUTO_INCREMENT,
url_title varchar(75) NOT NULL,
PRIMARY KEY (entry_id),
KEY (url_title)
) ENGINE=MyISAM;
INSERt INTO aaronlax (url_title) VALUES
('a-stoney'),('a-stoney'),
('ab-dille'),('ab-dille'),
('ab-dille'),('ac-gifta'),
('ae-jones'),('ae-jones');
SAMPLE DATA LOADED
mysql> use test
Database changed
mysql> DROP TABLE IF EXISTS aaronlax;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE aaronlax
-> (
-> entry_id int(10) unsigned NOT NULL AUTO_INCREMENT,
-> url_title varchar(75) NOT NULL,
-> PRIMARY KEY (entry_id),
-> KEY (url_title)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERt INTO aaronlax (url_title) VALUES
-> ('a-stoney'),('a-stoney'),
-> ('ab-dille'),('ab-dille'),
-> ('ab-dille'),('ac-gifta'),
-> ('ae-jones'),('ae-jones');
Query OK, 8 rows affected (0.00 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select * from aaronlax;
+----------+-----------+
| entry_id | url_title |
+----------+-----------+
| 1 | a-stoney |
| 2 | a-stoney |
| 3 | ab-dille |
| 4 | ab-dille |
| 5 | ab-dille |
| 6 | ac-gifta |
| 7 | ae-jones |
| 8 | ae-jones |
+----------+-----------+
8 rows in set (0.00 sec)
mysql>
PROPOSED SOLUTION EXECUTED
mysql> SET @ndx = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> SET @cur_hash = '';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT entry_id,CONCAT(url_title,IF(ndx>0,CONCAT('-',ndx),'')) url_title
-> FROM (SELECT
-> @new_hash := MD5(url_title),
-> (@ndx := IF(@new_hash=@cur_hash,@ndx+1,0)) ndx,
-> @cur_hash := MD5(url_title),
-> url_title,entry_id
-> FROM (SELECT url_title,entry_id FROM aaronlax ORDER BY url_title) AA) A;
+----------+------------+
| entry_id | url_title |
+----------+------------+
| 1 | a-stoney |
| 2 | a-stoney-1 |
| 3 | ab-dille |
| 4 | ab-dille-1 |
| 5 | ab-dille-2 |
| 6 | ac-gifta |
| 7 | ae-jones |
| 8 | ae-jones-1 |
+----------+------------+
8 rows in set (0.00 sec)
mysql>
GIVE IT A TRY !!!
CAVEAT
In case you are wondering why the proposed solution works, let's take a look at the subqueries
INNERMOST SUBQUERY
mysql> SELECT url_title,entry_id FROM aaronlax ORDER BY url_title;
+-----------+----------+
| url_title | entry_id |
+-----------+----------+
| a-stoney | 1 |
| a-stoney | 2 |
| ab-dille | 3 |
| ab-dille | 4 |
| ab-dille | 5 |
| ac-gifta | 6 |
| ae-jones | 7 |
| ae-jones | 8 |
+-----------+----------+
8 rows in set (0.00 sec)
mysql>
NEXT LEVEL SUBQUERY
mysql> SELECT
-> @new_hash := MD5(url_title),
-> (@ndx := IF(@new_hash=@cur_hash,@ndx+1,0)) ndx,
-> @cur_hash := MD5(url_title),
-> url_title,entry_id
-> FROM (SELECT url_title,entry_id FROM aaronlax ORDER BY url_title) AA;
+----------------------------------+------+----------------------------------+-----------+----------+
| @new_hash := MD5(url_title) | ndx | @cur_hash := MD5(url_title) | url_title | entry_id |
+----------------------------------+------+----------------------------------+-----------+----------+
| 911adc6db360f218da5069b20cfb1917 | 0 | 911adc6db360f218da5069b20cfb1917 | a-stoney | 1 |
| 911adc6db360f218da5069b20cfb1917 | 1 | 911adc6db360f218da5069b20cfb1917 | a-stoney | 2 |
| b468c0dd0ef78fde7cf5ae14765f9a5a | 0 | b468c0dd0ef78fde7cf5ae14765f9a5a | ab-dille | 3 |
| b468c0dd0ef78fde7cf5ae14765f9a5a | 1 | b468c0dd0ef78fde7cf5ae14765f9a5a | ab-dille | 4 |
| b468c0dd0ef78fde7cf5ae14765f9a5a | 2 | b468c0dd0ef78fde7cf5ae14765f9a5a | ab-dille | 5 |
| 3fff0367af8bf5d3b3b0089b8f187c04 | 0 | 3fff0367af8bf5d3b3b0089b8f187c04 | ac-gifta | 6 |
| 1909ba0f7a57ca081c82d2f7ba54c9e5 | 0 | 1909ba0f7a57ca081c82d2f7ba54c9e5 | ae-jones | 7 |
| 1909ba0f7a57ca081c82d2f7ba54c9e5 | 1 | 1909ba0f7a57ca081c82d2f7ba54c9e5 | ae-jones | 8 |
+----------------------------------+------+----------------------------------+-----------+----------+
8 rows in set (0.00 sec)
mysql>
From this level, I only need three columns: entry_id, url_title, ndx in the outer most level
Then, I append to the url_title a hyphen and ndx if ndx is nonzero
SHOW CREATE TABLE mytable\G