如何在Inner Join MySQL上订购数据?

时间:2018-05-04 10:35:12

标签: mysql mariadb

我有一个基于类别排序创建播客播放列表的案例。例如,我想根据下表加入播客数据和类别数据。

> SELECT * FROM category_sorts;

+----+-------------+------+
| id | category_id | sort |
+----+-------------+------+
|  1 |           2 |    1 |
|  2 |           1 |    2 |
|  3 |           1 |    3 |
|  4 |           2 |    4 |
+----+-------------+------+

与类别表相关的列category_id

> SELECT * FROM categories;

+----+--------------+-----------+
| id | title        | status    |
+----+--------------+-----------+
|  1 | Saga         | published |
|  2 | Asia Calling | published |
+----+--------------+-----------+

此处来自podcasts表的一些样本数据与相关类别。

> SELECT * FROM podcasts;

+----+-------------+------------------------+---------------------+
| id | category_id | title                  | published_at        |
+----+-------------+------------------------+---------------------+
|  1 |           1 | Sejarah Rumah Dengklok | 2018-05-01 22:40:47 |
|  3 |           2 | Berita Asia!           | 2018-05-01 22:52:15 |
|  4 |           2 | Asia Later!            | 2018-05-01 23:47:13 |
|  5 |           1 | Saga terbaru           | 2018-05-02 00:06:32 |
+----+-------------+------------------------+---------------------+

使用下面提供的查询,我在同一类别中获得了重复的播客。

SELECT
        p.id, p.title AS podcast_title, p.published_at, 
        c.title AS category_title,
        cs.sort
    FROM podcasts as p
    INNER JOIN categories as c ON c.id = p.category_id
    INNER JOIN category_sorts AS cs on cs.category_id = c.id
    GROUP BY cs.sort
    ORDER BY cs.sort ASC, p.published_at DESC

结果:

+----+------------------------+---------------------+----------------+------+
| id | podcast_title          | published_at        | category_title | sort |
+----+------------------------+---------------------+----------------+------+
|  3 | Berita Asia!           | 2018-05-01 22:52:15 | Asia Calling   |    1 |
|  1 | Sejarah Rumah Dengklok | 2018-05-01 22:40:47 | Saga           |    2 |
|  1 | Sejarah Rumah Dengklok | 2018-05-01 22:40:47 | Saga           |    3 |
|  3 | Berita Asia!           | 2018-05-01 22:52:15 | Asia Calling   |    4 |
+----+------------------------+---------------------+----------------+------+

我期待什么?

我想根据sort列和published_at列指示的最新播客订购播放列表。使用sort列的订单没问题,但我无法在播客的published_at日期订购。

预期结果:

+----+------------------------+---------------------+----------------+------+
| id | podcast_title          | published_at        | category_title | sort |
+----+------------------------+---------------------+----------------+------+
|  4 | Asia Later!            | 2018-05-01 22:52:15 | Asia Calling   |    1 |
|  5 | Saga Terbaru           | 2018-05-01 22:40:47 | Saga           |    2 |
|  1 | Sejarah Rumah Dengklok | 2018-05-01 22:40:47 | Saga           |    3 |
|  3 | Berita Asia!           | 2018-05-01 22:52:15 | Asia Calling   |    4 |
+----+------------------------+---------------------+----------------+------+

有人能给我一个如何处理它的线索吗?任何链接或来源将不胜感激。

Here is示例全表结构和来自SQL Fiddle的数据(如果你想玩它)。

谢谢。

1 个答案:

答案 0 :(得分:1)

如果您不需要多个类别,请在您的帖子下查看我的评论,但如果您想允许多个类别,因为示例似乎暗示尝试下面的sql。唯一的变化是按category_id进行分组,因此您不会重复:

SELECT
        p.id, p.title AS podcast_title, p.published_at, 
        c.title AS category_title,
        cs.sort
    FROM podcasts as p
    INNER JOIN categories as c ON c.id = p.category_id
    INNER JOIN category_sorts AS cs on cs.category_id = c.id
    GROUP BY cs.category_id
    ORDER BY cs.sort ASC, p.published_at DESC

或实际上为了使其更加防弹,引入聚合min(cs.sort) as sorting_order,因此始终正确选择订单并在ORDER子句中使用:

SELECT
        p.id, p.title AS podcast_title, p.published_at, 
        c.title AS category_title,
        min(cs.sort) as sorting_order
    FROM podcasts as p
    INNER JOIN categories as c ON c.id = p.category_id
    INNER JOIN category_sorts AS cs on cs.category_id = c.id
    GROUP BY cs.category_id
    ORDER BY sorting_order ASC, p.published_at DESC

但是如果你想保持类别重复并摆脱播客重复,那么:

SELECT
        p.id, p.title AS podcast_title, p.published_at, 
        c.title AS category_title,
        cs.sort
    FROM podcasts as p
    INNER JOIN categories as c ON c.id = p.category_id
    INNER JOIN category_sorts AS cs on cs.category_id = c.id
    GROUP BY p.id
    ORDER BY cs.sort ASC, p.published_at DESC 

在这里小提琴:http://sqlfiddle.com/#!9/3672ce/22