带连接的子查询 - 即使为空也需要帮助返回行 - MySQL

Subquery with joins - need help returning rows even if empty - MySQL

好吧伙计们,这是一个非常令人困惑的大问题。

我正在创建一个 dealio 类型的论坛:我有一个主题数组,每个主题都有一个与该主题相关联的 Post 数组。主题列表页面应显示该特定主题的最新 Post。 (每个主题也属于一个类别)

Topic Latest post
Check out my page John Smith - 10:00 PM

MYSQL
select `ct`.*, `a`.`username`, `x`.* from `community-topics` as `ct` 
inner join `community-categories` as `cc` on `cc`.`communityCategoryId` = `ct`.`refCategoryId` 
inner join `authentication` as `a` on `a`.`userId` = `ct`.`refCreatedBy` 
inner join (
    select `a2`.`username` AS lastPostUsername, `cp`.`createdAt` AS lastPostCreatedAt, 
    `cp`.`body`, `cp`.`refTopicId` from `community-posts` as `cp` 
    inner join `authentication` as `a2` on `a2`.`userId` = `cp`.`refCreatedBy`
    order by `cp`.`createdAt` desc limit 1
) as x on `x`.`refTopicId` = `ct`.`communityTopicId`
where `cc`.`name` = 'general' order by `ct`.`createdAt` desc

这个查询实际上按照我需要的方式工作。唯一的问题是如果子查询为空,行将不会 return。我仍然想要 return 子查询之外的所有数据,即使子查询为 null/false/empty。我试过使用 IFNULL / ISNULL,但我认为我没有正确地使用子查询中的附加连接。

如果我有 2 个主题,1 个主题有 5 个帖子,1 个主题有 0 个帖子,只有 5 个帖子的主题会显示,而 0 个帖子的主题不会显示,因为子查询 return 是空的。

如果该行为空,我想 return 向前端用户显示类似“抱歉,没有最近的帖子”之类的内容。

任何帮助都会很棒!

如果您想要在没有匹配项的情况下保留行,您需要 left join

select `ct`.*, `a`.`username`, `x`.*
from `community-topics` `ct` inner join
     `community-categories` `cc`
      on `cc`.`communityCategoryId` = `ct`.`refCategoryId` inner join 
      `authentication` `a`
      on `a`.`userId` = `ct`.`refCreatedBy` left join
      (select `a2`.`username` AS lastPostUsername, `cp`.`createdAt` AS lastPostCreatedAt, 
             `cp`.`body`, `cp`.`refTopicId`
       from `community-posts` as `cp` left join
            `authentication` as `a2`
             on `a2`.`userId` = `cp`.`refCreatedBy`
       order by `cp`.`createdAt` desc limit 1
      )  x
      on `x`.`refTopicId` = `ct`.`communityTopicId`
where `cc`.`name` = 'general'
order by `ct`.`createdAt` desc;

我还建议您删除所有反引号。它们只会让查询更难编写和阅读。

编辑:

如果我理解你的评论:

select `ct`.*, `a`.`username`, `x`.*
from `community-topics` `ct` inner join
     `community-categories` `cc`
      on `cc`.`communityCategoryId` = `ct`.`refCategoryId` inner join 
      `authentication` `a`
      on `a`.`userId` = `ct`.`refCreatedBy` left join
      (select `a2`.`username` AS lastPostUsername, `cp`.`createdAt` AS lastPostCreatedAt, 
             `cp`.`body`, `cp`.`refTopicId`,
             row_number() over (partition by refTopicId order by cp.created_at desc) as seqnum
       from `community-posts` as `cp` left join
            `authentication` as `a2`
             on `a2`.`userId` = `cp`.`refCreatedBy`
      )  x
      on `x`.`refTopicId` = `ct`.`communityTopicId` and seqnum = 1
where `cc`.`name` = 'general'
order by `ct`.`createdAt` desc;