【问题标题】:How to get total number of rows, and latest row's value in MySql?如何获取MySql中的总行数和最后一行值?
【发布时间】:2022-01-03 12:20:54
【问题描述】:

我有两个名为 PostsComments 的表。

帖子表的样子。

|id|user_id|content|created_at|updated_at|
|1 | 24    |demotxt|demo_date |demo_date |
|2 | 21    |domotxt|demo_date2|demo_date2|
|3 | 24    |domotxt|demo_date2|demo_date3|
|4 | 28    |dimotxt|demo_date3|demo_date5|

评论表看起来像

|id|user_id|post_id|comment |created_at|updated_at|
|1 | 24    |  3    |comment1|demo_date |demo_date |
|2 | 21    |  3    |xyadbsss|demo_date2|demo_date2|
|3 | 24    |  1    |okayokay|demo_date2|demo_date3|
|4 | 28    |  4    |somehtin|demo_date3|demo_date5|

我想要实现的是获得第一个最新评论和每个帖子的 cmets 总数。

|post_id|latest_comment |total_comments |
| 3     |xyadbsss       |2              |
| 1     |okayokay       |1              |
| 4     |somehtin       |1              |

这是我试过的sql查询

SELECT post_id, count(post_id) total_comments, comment latest_comment
FROM `comments` 
  LEFT JOIN posts on comments.post_id = posts.id
GROUP BY post_id;

这是给我的

|post_id|latest_comment |total_comments |
| 3     |comment1[not latest]|2         |
| 1     |okayokay       |1              |
| 4     |somehtin       |1              |

【问题讨论】:

  • 使用 eloquent 关系 + withCount 获取 cmets 的计数。使用另一个with('latestComment') 并在后模型上定义这两个关系。
  • @BhaumikPandhi 没有其他办法。我的意思是我已经使用了 withCount 但无论如何我都不能使用 count 来操纵查询以获取最新评论。
  • 你使用的是什么版本的 Laravel?另外,您是否为postscomments 设置了模型/关系?
  • @Rwd 我正在使用 Laravel 8 。是的,所有关系都是针对 PostComment 模型
  • 要获得将多行合并为一行的结果所需的计数。一旦发生这种情况,您将丢失有关单个行的所有信息,因此唯一的方法是通过子查询或使用"windowing",它的作用类似于分组但不会将组折叠到单行。我一直觉得“GROUP BY”是一个非常令人困惑的名字,我认为称它为“AGGREGATE ON”会消除很多困惑

标签: mysql sql laravel


【解决方案1】:

你能试试这样的方法吗,不确定是否是最好的解决方案。

SELECT 
c1.post_id,
count(c1.`post_id`) total_comments,
(
    SELECT 
        c2.comment
    FROM
        comments as c2
    WHERE
        c2.post_id = c1.`post_id`
    ORDER BY 
        c2.id DESC LIMIT 1
) as latest_comment
FROM 
`comments` as c1
GROUP BY 
c1.`post_id`;

我宁愿循环第一个数据并根据帖子ID进行另一个查询以获取最新评论。

【讨论】:

  • 评论重复了。
  • 嗯,你能重试我编辑的查询
  • 是的,这工作正常,但它似乎不是优化的解决方案。
  • 很好,是的,就像我说的不确定是否是最好的解决方案,我看到一个新人评论了如何在 laravel 中使用,所以如果你使用 laravel,我建议使用他的解决方案。祝你好运
【解决方案2】:

要获取数据库中的最新记录,您可以使用它

Model::latest()->first();

所以在你的情况下获得最新评论:

Comment::latest()->first()['comment'];

您也可以根据您的 laravel 版本使用此方法(如果您使用 5.7 或更高版本):

DB::table('comments')->latest('comment')->first();

现在要计算帖子上的所有 cmets,您可以查找具有帖子 ID 的所有 cmets 并使用 count 属性

$count = Comment::where('post_id',$the_id_of_the_post)->count();

【讨论】:

    【解决方案3】:

    实现此目的的一种方法是使用latestOfMany 方法。将以下内容添加到您的 Post 模型中:

    public function latestComment()
    {
        return $this->hasOne(Comment::class)->latestOfMany();
    }
    

    然后你可以预先加载结果:

    $posts = Post::with('latestComment')->withCount('comments as total_comments')->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2021-05-29
      • 2022-07-07
      • 2019-03-09
      相关资源
      最近更新 更多