mysql - 查询不起作用需要在 groupby 之前 orderby 吗?

标签 mysql sql greatest-n-per-group

这应该是一个主题列表,其中发布了新附件。

此查询中的group by 未按我希望的方式工作。 (这是 SMF 。) 它应该从每个具有最近附件的线程中提取一个代表性附件 - 不仅仅是新线程,而是任何线程。结果将是一张缩略图,链接到发布相应新图像的线程。

相反,它不起作用。目前有 2 个问题。第一个问题是它显示线程中的第一个附件而不是最新的附件。第二个问题是它按线程创建日期排序。我希望附件创建日期为顺序,并且线程列表根据哪些线程中包含最新附件进行排序。因此,即使旧线程有最近的附件,也不会上升到顶部。我需要先进行子选择才能订购吗?我怎样才能做到这一点?

提前非常感谢您。

SELECT *
FROM smf_attachments AS att
    INNER JOIN smf_messages AS m ON (m.id_msg = att.id_msg)
    INNER JOIN smf_topics AS t ON (t.id_topic = m.id_topic)
    LEFT JOIN smf_members AS mem ON (mem.id_member = m.id_member)
    LEFT JOIN smf_attachments AS thumb ON (thumb.id_attach = att.id_thumb)
WHERE att.attachment_type = 0
    AND t.id_board = 3

GROUP BY t.id_topic         
ORDER BY att.id_attach  DESC
LIMIT 2

(编辑。)

假设我们有 3 个 topic_id,每个 topic_id 有 5 个附件。较高的附件编号意味着它是较新的附件,较高的 topic_id 意味着它是较新的主题。

topic_id 1|attachment 3,6,13,14,15
topic_id 2|attachment 1,2,4,5,12
topic_id 3|attachment 7,8,9,10,11

在上述情况下,查询应提取 2 条记录(限制 2 条):

topic_id  attachment
--------  ----------
1         15
2         12

由于我只想每个主题 1 个附件,因此具有最新附件的 2 个主题是主题 1 和 2(分别具有附件 15 和 12)。尽管主题 3 较新,但在本例中它不会成为结果,因为它的附件较旧。

(编辑。)

我尝试了 systemmatrix 提供的解决方案,但它仍然按 topic_id 创建日期排序,而不是按哪些主题包含最新附件。我试图让我上面的问题更清楚。有什么帮助吗?预先感谢您。

(编辑) 我现在看到它按每个主题中第一个附件的日期排序。最近第一个附件的主题。我需要带有最新附件或最后一个附件的主题

编辑 我做了一个更改,为我提供了正确的主题列表。这是新代码。

            SELECT att.id_thumb, att.id_msg, att.attachment_type, att.id_attach, distinct on (t.id_topic)
            FROM smf_attachments AS att
                INNER JOIN smf_messages AS m ON (m.id_msg = att.id_msg)
                INNER JOIN smf_topics AS t ON (t.id_topic = m.id_topic)
                LEFT JOIN smf_members AS mem ON (mem.id_member = m.id_member)
                LEFT JOIN smf_attachments AS thumb ON (thumb.id_attach = att.id_thumb)
            WHERE att.attachment_type = 0
                AND t.id_board = 3


            ORDER BY max(att.id_attach)  DESC
            LIMIT 2

现在唯一的问题是如何让它向我显示正确的缩略图附件?它仍然显示相应主题中的第一个附件,而不是最后一个。有什么帮助吗? 预先感谢您。

最佳答案

如果只有这些 3 列,您需要每个主题的最新记录

  • att.id_thumb
  • att.id_msg
  • att.attachment_type

您可以使用 SUBSTRING_INDEX 的技巧超过 GROUP_CONCAT 的结果如果想要获取按 id_attach 排序的主题的最新 id_thumb,您可以将 id_thumb 的表达式编写为

SUBSTRING_INDEX(
GROUP_CONCAT(att.id_thumb ORDER BY att.id_attach DESC SEPARATOR '||'),
'||',1) AS id_thumb 
<小时/>
  • GROUP_CONCAT(expr)

This will group all the id_thumbs for a topic and provide you the list separated by double || or you can change the separator as per your needs in above i have use || for e.g result for above function will be like thumb1||thumb2||thumb3||thumb4 this will be ordered by id_attach in descending way.

Note The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet.

<小时/>
  • SUBSTRING_INDEX(str,delim,count)

From docs Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

<小时/>

现在您的最终查询将如下所示

SELECT 
SUBSTRING_INDEX(GROUP_CONCAT(att.id_thumb ORDER BY att.id_attach DESC SEPARATOR '||'),'||',1) AS id_thumb,
SUBSTRING_INDEX(GROUP_CONCAT(att.id_msg ORDER BY att.id_attach DESC SEPARATOR '||'),'||',1) AS id_msg,
SUBSTRING_INDEX(GROUP_CONCAT(att.attachment_type ORDER BY att.id_attach DESC SEPARATOR '||'),'||',1) AS attachment_type,
MAX(att.id_attach) AS id_attach_max,
t.id_topic
FROM
  smf_attachments AS att 
  INNER JOIN smf_messages AS m ON (m.id_msg = att.id_msg) 
  INNER JOIN smf_topics AS t ON (t.id_topic = m.id_topic) 
  LEFT JOIN smf_members AS mem ON (mem.id_member = m.id_member) 
  LEFT JOIN smf_attachments AS thumb ON (thumb.id_attach = att.id_thumb) 
WHERE att.attachment_type = 0 
  AND t.id_board = 3 
GROUP BY  t.id_topic
ORDER BY id_attach_max DESC 
LIMIT 2 

关于mysql - 查询不起作用需要在 groupby 之前 orderby 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25540820/

相关文章:

mysql - 如何组合三个选择查询,每个查询都排除前一个查询的结果?

PHP版本以及与Mysql 8的兼容性

mysql - 带连接的sql更新

php - 数据库查询的 IF 语句不会执行

mysql - 从另外两个表更新第三个表

一起使用 MIN 和 MAX 的 SQL 函数

mysql - 如何更改列中具有两位数字的 VARCHAR 值(例如 : 0214 month year) to a date format in MySQL?

mysql - 如何在 MYSQL 中通过另一列选择具有 MAX(列值)、PARTITION 的行?

mysql - 获取每组分组结果的前n条记录

sql - 如何为每个键值选择具有最新时间戳的行?