订购和分组MySQL数据

时间:2011-05-13 13:16:53

标签: group-by sql-order-by

+----+------------------+-----------------+
| id | template_type_id | url             |
+----+------------------+-----------------+
|  1 |       1          | text            |
|  2 |       2          | text            |
|  3 |       1          | text            |
|  4 |       1          | text            |
|  5 |       1          | text            |
|  6 |       1          | text            |
|  7 |       1          | text            |
|  8 |       1          | text            |
|  9 |       2          | text            |
| 10 |       2          | text            |
+----+------------------+-----------------+

由于我使用1页模板和2页模板,我需要按照1页和2页重新排序上述结果,如下所示:

+----+------------------+-----------------+
| id | template_type_id | url             |
+----+------------------+-----------------+
|  1 |       1          | text            |
|  3 |       1          | text            |
|  2 |       2          | text            |
|  4 |       1          | text            |
|  5 |       1          | text            |
|  6 |       1          | text            |
|  7 |       1          | text            |
|  9 |       2          | text            |
| 10 |       2          | text            |
|  8 |       1          | text            |
+----+------------------+-----------------+
+------------------------------------------+
   ----------------  ------------------
   |               | |                 |
   |               | |                 |
   |               | |                 |
   |               | |                 |
 
   |               | |                 |
   |               | |                 |
   |               | |                 |
   |               | |                 |
   |               | |                 |
   ----------------  ------------------
+------------------------------------------+

1 个答案:

答案 0 :(得分:0)

假设表中的publish_date列未显示且其中的值与示例1和2中的记录顺序一致,我建议:

order by publish_date, template_type_id