重复的内连接结果

发布于 2025-01-15 10:00:49 字数 2291 浏览 2 评论 0原文

使用 postgres,我试图获取单个用户及其所有帖子,但是当我内部加入 Users 和 Posts 表时,我收到以下信息:

-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------
PhotoPath   | 
Username    | username
Name        | user
Biography   | 
PicturePath | https://gourmetdemexico.com.mx/wp-content/uploads/2020/05/diferencia-entre-arte-y-cultura-taco-gringo-600x600.jpg
-[ RECORD 2 ]------------------------------------------------------------------------------------------------------------------
PhotoPath   | 
Username    | username
Name        | user
Biography   | 
PicturePath | http://www.travelettes.net/wp-content/uploads/2012/06/mystery2-600x600.jpg

QUERY:

SELECT 
    u."PhotoPath",
    u."Username",
    u."Name",
    u."Biography",
    p."PicturePath"
FROM "Users" as u 
INNER JOIN "Posts" as p ON u."ID" = p."UserID"
WHERE p."UserID" = 9;

我也尝试做同样的事情,但作为 json

-[ RECORD 1 ]----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
user | [{"photo" : null, "username" : "username", "name" : "user", "bio" : null}, {"photo" : null, "username" : "username", "name" : "user", "bio" : null}]
pics | {https://gourmetdemexico.com.mx/wp-content/uploads/2020/05/diferencia-entre-arte-y-cultura-taco-gringo-600x600.jpg,http://www.travelettes.net/wp-content/uploads/2012/06/mystery2-600x600.jpg}

QUERY:

SELECT json_agg(json_build_object(
    'photo', u."PhotoPath",
    'username', u."Username",
    'name', u."Name",
    'bio', u."Biografy"
)) as user,
array_agg(p."PicturePath") as pics
FROM "Users" as u
INNER JOIN "Posts" as p
ON u."ID" = p."UserID"
WHERE u."ID" = 9
GROUP BY p."UserID";

我知道为什么但我不知道如何避免它,有人可以帮助我得到这样的东西:

user | {"photo" : null, "username" : "username", "name" : "user", "bio" : null}, 
pics | [https://gourmetdemexico.com.mx/wp-content/uploads/2020/05/diferencia-entre-arte-y-cultura-taco-gringo-600x600.jpg,http://www.travelettes.net/wp-content/uploads/2012/06/mystery2-600x600.jpg]

With postgres I'm trying to get a single user and all their posts, but when I inner join the Users and Posts tables I receive this:

-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------
PhotoPath   | 
Username    | username
Name        | user
Biography   | 
PicturePath | https://gourmetdemexico.com.mx/wp-content/uploads/2020/05/diferencia-entre-arte-y-cultura-taco-gringo-600x600.jpg
-[ RECORD 2 ]------------------------------------------------------------------------------------------------------------------
PhotoPath   | 
Username    | username
Name        | user
Biography   | 
PicturePath | http://www.travelettes.net/wp-content/uploads/2012/06/mystery2-600x600.jpg

QUERY:

SELECT 
    u."PhotoPath",
    u."Username",
    u."Name",
    u."Biography",
    p."PicturePath"
FROM "Users" as u 
INNER JOIN "Posts" as p ON u."ID" = p."UserID"
WHERE p."UserID" = 9;

I'm also trying to do the same thing but as a json

-[ RECORD 1 ]----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
user | [{"photo" : null, "username" : "username", "name" : "user", "bio" : null}, {"photo" : null, "username" : "username", "name" : "user", "bio" : null}]
pics | {https://gourmetdemexico.com.mx/wp-content/uploads/2020/05/diferencia-entre-arte-y-cultura-taco-gringo-600x600.jpg,http://www.travelettes.net/wp-content/uploads/2012/06/mystery2-600x600.jpg}

QUERY:

SELECT json_agg(json_build_object(
    'photo', u."PhotoPath",
    'username', u."Username",
    'name', u."Name",
    'bio', u."Biografy"
)) as user,
array_agg(p."PicturePath") as pics
FROM "Users" as u
INNER JOIN "Posts" as p
ON u."ID" = p."UserID"
WHERE u."ID" = 9
GROUP BY p."UserID";

I know why but I don't know how to avoid it, can someone help me to get something like this:

user | {"photo" : null, "username" : "username", "name" : "user", "bio" : null}, 
pics | [https://gourmetdemexico.com.mx/wp-content/uploads/2020/05/diferencia-entre-arte-y-cultura-taco-gringo-600x600.jpg,http://www.travelettes.net/wp-content/uploads/2012/06/mystery2-600x600.jpg]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2025-01-22 10:00:49

通过这样做,我解决了这个问题:

WITH cte(j) AS (
    SELECT 
        jsonb_build_object(
            'photo', "PhotoPath",
            'username', "Username",
            'name', "Name",
            'bio', "Biography",
            'posts', array_agg(
                "Posts"."PicturePath"
            )
        )
        FROM "Users"
        INNER JOIN "Posts" ON "Posts"."UserID" = "Users"."ID"
        WHERE "Users"."ID" = @
        GROUP BY "Users"."ID"
)
SELECT json_agg(j) as user
FROM cte;

By doing this I resolve the issue:

WITH cte(j) AS (
    SELECT 
        jsonb_build_object(
            'photo', "PhotoPath",
            'username', "Username",
            'name', "Name",
            'bio', "Biography",
            'posts', array_agg(
                "Posts"."PicturePath"
            )
        )
        FROM "Users"
        INNER JOIN "Posts" ON "Posts"."UserID" = "Users"."ID"
        WHERE "Users"."ID" = @
        GROUP BY "Users"."ID"
)
SELECT json_agg(j) as user
FROM cte;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文