重复的内部连接结果
Duplicated inner join results
使用 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
查询:
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}
查询:
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 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;
使用 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
查询:
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}
查询:
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 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;