mysql - 多重订购 是否可以按日期订购的模块 id 带来最新(仅一篇)文章?例子

标签 mysql sql sql-order-by

我有这个查询,我想从复杂的连接中获取最新和最新的文章,同时 按模块排序。

我向 stackoverflow 专家提出的问题如下。

是否可以按日期排序的模块 ID 带来最新(仅一篇)文章?

SELECT *
FROM articles Article
LEFT JOIN sources Source ON Article.source_id = Source.id
RIGHT JOIN app_sources asrc ON asrc.source_id = Source.id
/*GROUP BY asrc.module*/ -> if I enable this i get only one but no order by date
ORDER BY asrc.module ASC, Article.published_at DESC

这是我的第一个问题,我希望我的格式正确! 非常感谢

目前是这样(2k 个结果)

published_at    title   module
2012-08-22 12:16:41 |   Archos Gen10: Tablet Productivity | 1
2012-08-22 12:13:22 | Vail Resorts Ski App Gets Racing With Lindsey Vonn |  1
2012-08-22 11:58:06 | The Internet a Decade Later [INFOGRAPHIC] |   1

我想要这样

published_at    title   module
2012-08-15 14:37:40 | The Air Force’s New Ultra-Fast Jet Has an Epic Fai... | 1
2012-01-13 16:17:51 | Canada’s Helium Digital shows us the thinnest iPad... | 2
2012-01-13 14:40:14 | ESPN Feels Lonely: A Chat Regarding ESPN’s Role In... | 3

最佳答案

如果没有看到您的表结构,其中很多内容都将是猜测。但似乎您需要在 published_at 字段中合并 MAX()

类似这样的事情:

SELECT *
FROM
(
    SELECT max(published_at) maxDate, source_id
    FROM articles
    GROUP BY source_id
) Article
LEFT JOIN sources Source 
    ON Article.source_id = Source.id
LEFT JOIN app_sources asrc 
    ON Source.id = asrc.source_id
ORDER BY asrc.module ASC, Article.maxDate DESC

如果您想要更明确的答案,则需要提供更多详细信息。

关于mysql - 多重订购 是否可以按日期订购的模块 id 带来最新(仅一篇)文章?例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080618/

相关文章:

mysql - 多个唯一列,但只有唯一组合

php - 我们不能在 php 中调用 $result 作为变量吗

mysql - Wordpress Transfer Broke CSS - 图像+链接工作

sql - 根据同一表中的行计算 SQL 存储过程中的总计

mysql - 按优先级排序而不是按字母顺序

带有自定义比较器的 Linq orderby 两列排序

mysql - 为什么这个 IN(内部查询)查询不返回任何结果?

sql - 'AS'在SQL中是什么意思?

mysql - 条件写在 Where 子句上好还是 ON 子句好?

mysql - ORDER BY 无法按预期使用 GROUP BY