重复的内连接结果
使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过这样做,我解决了这个问题:
By doing this I resolve the issue: