想象一下,你有 2 个这样的表:
mysql> SELECT * FROM theme;
+----+---------+------------+
| id | name | sort_order |
+----+---------+------------+
| 1 | Theme 1 | 1 |
| 2 | Theme 2 | 2 |
| 3 | Theme 3 | 3 |
| 4 | Theme 4 | 4 |
| 5 | Theme 5 | 5 |
| 6 | Theme 6 | 6 |
| 7 | Theme 7 | 7 |
+----+---------+------------+
mysql> SELECT * FROM article;
+----+------------+---------------------+----------+
| id | title | update_date | theme_id |
+----+------------+---------------------+----------+
| 1 | Article 1 | 2012-06-29 15:29:50 | 6 |
| 2 | Article 2 | 2012-07-18 00:00:00 | 2 |
| 3 | Article 3 | 2012-07-19 00:00:00 | 4 |
| 4 | Article 4 | 2012-07-18 00:00:00 | 4 |
| 5 | Article 5 | 2012-07-18 00:00:00 | 1 |
| 6 | Article 6 | 2012-06-26 10:30:51 | 6 |
| 7 | Article 7 | 2012-07-18 15:17:08 | 6 |
| 8 | Article 8 | 2012-06-18 00:00:00 | 4 |
| 9 | Article 9 | 2012-07-18 15:48:28 | 1 |
| 10 | Article 10 | 2012-07-09 00:00:00 | 4 |
+----+------------+---------------------+----------+
每篇文章都绑定一个且只有一个主题。
您希望能够执行一个查询,为您提供如下排序的文章列表:
- 按主题排序的每个主题的第一篇最新文章
sort_order
- 按主题排序的每个主题的第二篇最新文章
sort_order
- 按主题排序的每个主题的第三篇最新文章
sort_order
- 等等...
对于当前数据,它应该给出以下内容:
+----+------------+---------------------+----------+
| id | title | update_date | theme_id |
+----+------------+---------------------+----------+
| 9 | Article 9 | 2012-07-18 15:48:28 | 1 |
| 2 | Article 2 | 2012-07-18 00:00:00 | 2 |
| 3 | Article 3 | 2012-07-19 00:00:00 | 4 |
| 7 | Article 7 | 2012-07-18 15:17:08 | 6 |
| 5 | Article 5 | 2012-07-18 00:00:00 | 1 |
| 4 | Article 4 | 2012-07-18 00:00:00 | 4 |
| 1 | Article 1 | 2012-06-29 15:29:50 | 6 |
| 10 | Article 10 | 2012-07-09 00:00:00 | 4 |
| 6 | Article 6 | 2012-06-26 10:30:51 | 6 |
| 8 | Article 8 | 2012-06-18 00:00:00 | 4 |
+----+------------+---------------------+----------+
我几乎可以肯定有一种方法可以使用单个查询来做到这一点,但我无法弄清楚。
您将如何实现这一目标?