呵呵, 不要发重复的帖子 mysql> SELECT * FROM `t1`; +----+------+ | id | name | +----+------+ | 1 | aa | | 2 | bb | | 3 | cc | | 4 | aa | +----+------+ 4 rows in set (0.03 sec)mysql> SELECT * FROM `t1` ORDER BY `name`; +----+------+ | id | name | +----+------+ | 1 | aa | | 4 | aa | | 2 | bb | | 3 | cc | +----+------+ 4 rows in set (0.00 sec)mysql> SET @ID := 0; Query OK, 0 rows affected (0.00 sec)mysql> SELECT (@ID := @ID + 1) AS SortId, id, name FROM `t1` ORDER BY `name`; +--------+----+------+ | SortId | id | name | +--------+----+------+ | 1 | 1 | aa | | 2 | 4 | aa | | 3 | 2 | bb | | 4 | 3 | cc | +--------+----+------+ 4 rows in set (0.00 sec)
不要发重复的帖子
mysql> SELECT * FROM `t1`;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | aa |
+----+------+
4 rows in set (0.03 sec)mysql> SELECT * FROM `t1` ORDER BY `name`;
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 4 | aa |
| 2 | bb |
| 3 | cc |
+----+------+
4 rows in set (0.00 sec)mysql> SET @ID := 0;
Query OK, 0 rows affected (0.00 sec)mysql> SELECT (@ID := @ID + 1) AS SortId, id, name FROM `t1` ORDER BY `name`;
+--------+----+------+
| SortId | id | name |
+--------+----+------+
| 1 | 1 | aa |
| 2 | 4 | aa |
| 3 | 2 | bb |
| 4 | 3 | cc |
+--------+----+------+
4 rows in set (0.00 sec)