简体   繁体   English

如何从另一个相关表中选择最后2个条目

[英]How to select the last 2 entries from another related table

I have query that selects all users based on their sum of donation from another table and orders it by the sum of their donations. 我有一个查询,该查询根据他们在另一张表中的捐款总额来选择所有用户,并按其捐款总额对其进行排序。

But I would also like to select each user's last 2 comments where comment type is donation_comment by joining the them together with a space. 但我也想通过将每个用户的最后2条评论与一个空格连接在一起,选择评论类型为donation_comment的评论。 And to be also able to search by user comments. 并且还能够通过用户评论进行搜索。 If I specify where comment_text contains 'comment three' then only 1 entry would show for Sergey Brin. 如果我指定comment_text包含“注释三”的位置,那么Sergey Brin仅显示1个条目。

I can't seem to figure out how to pull in their last comments and add where condition based on it. 我似乎无法弄清楚如何提取他们的最后评论并在此基础上添加条件。

So the result would be this 所以结果就是这样

Array
    (
        [0] => stdClass Object
            (
                [id] => 2
                [username] => Sergey Brin
                [donation] => 500
                [last_comments] => comment four comment three
            )

        [1] => stdClass Object
            (
                [id] => 1
                [username] => Larry Page
                [donation] => 400
                [last_comments] => comment five comment two
            )
    )

Here's my current query 这是我当前的查询

SELECT
    users.id,
    users.username,
    sum(donations.donation) as donation
from
    users
inner join donations
    on users.id = donations.user_id
where
    users.username like '%r%'
group by
    users.id,
    users.username
having
    sum(donations.donation) >= 400
order by
    donation desc

users table 用户

id |   username   |
1     Larry Page
2     Sergey Brin

donations table 捐款

id | user_id | donation |     date      |
1      1         100       2019-02-12
2      1         200       2019-02-13
3      2         500       2019-01-15
4      1         100       2019-04-10

user_comments table user_comments

id | user_id |   comment_text   |        type        |
1       1        comment one       donation_comment
2       1        comment two       donation_comment
3       2        comment three     donation_comment
4       2        comment four      donation_comment 
5       1        comment five      donation_comment

I would make a subquery from your user_comments table where you limit the number of comments per user_id to 2. Then you can use string_agg() to concatinate the comments 我将从您的user_comments表中进行子查询,在该表中将每个user_id的评论数限制为2。然后可以使用string_agg()来添加评论

Try this: 尝试这个:

SELECT
    users.id,
    users.username,
    sum(donations.donation) as donation,
    string_agg(comment_text, ', ') as comments
from
    users
inner join donations
    on users.id = donations.user_id
inner join (
    SELECT* from user_comments
    group by user_id
    limit 2
    ) as last2_don on users.id = last2_don.user_id
where
    users.username like '%r%'
group by
    users.id,
    users.username
having
    sum(donations.donation) >= 400
order by
    donation desc

A lateral join is a very reasonable approach: 横向联接是一种非常合理的方法:

select u.id, u.username,
       sum(d.donation) as donation,
       uc.comments_2
from users u inner join
     donations d
     on u.id = d.user_id left join lateral
     (select string_agg(comment_text, '; ') as comments_2
      from (select uc.*
            from user_comments uc
            where uc.user_id = u.id and
                  uc.type = 'donation_comment';
            order by uc.id desc
            limit 2
           ) c
      ) uc
      on 1=1
where u.username like '%r%'
group by u.id, u.username, uc.comments
having sum(d.donation) >= 400
order by sum(donation) desc;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 另一个表中的最新相关条目 - Most recent related entries from another table 如何 select 表中的所有条目,其中子查询按第一个查询中的 id 计数另一个表中的条目 - How to select all entries in a table with a subquery counting entries in another table by id from the first query 如何选择上个月的所有条目? - How to select all the entries from the last month? SQL:如何从联接到另一个表中选择最后插入的记录 - SQL : how to select the last inserted record from a table joined to another 如何从一个表中选择一行,并从另一个(相关)表中选择许多行 - How to select a row from a table and many rows from another (related) table 从一个表中选择所有条目,并从另一个“日志”表中选择“最新”条目 - Select all entries from a table and the LATEST entries from another “logging” table 从表中选择第二个表中没有相关条目的行 - SELECT rows from a table that don't have related entries in a second table 从一个表中选择所有条目,在另一个表中有两个特定条目 - Select all entries from one table which has two specific entries in another table 如何从一个表中选择最后一行,然后更新另一个表的最后一行? - How to select the last row from one table then update the last row of another one? 如何 select 全部从一个表中按最后创建的时间戳从另一个表中的值排序 - How to select all from one table order by the last created timestamp of values from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM