- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个表posts、comments 和users。 posts 表包括两种类型的帖子,question 和answer。对问题和答案进行评论。我的目的是获得一个带有评论、答案和答案评论的问题。我还需要用户表中的用户名作为我获取的每个问题、答案和评论的作者。我正在使用 Postgres 9.5,并使用 json_agg()
函数。
虽然我需要的示例输出应该类似于以下第一个,但我得到了重复的条目。
我在这里缺少什么?正确的 group by 子句可能是。或者用他们的评论收集答案的子查询不是这样做的方法。当我从评论表上的帖子中注释掉左连接时,我得到了想要的结果,但没有对问题发表评论。此外,当我取消包含子查询的左连接时,我得到了预期的非重复结果,而且这也不是我想要的完整数据集。这些是我迄今为止为解决我的问题而收集的东西。
我需要什么:
[
{
"post_id": "10",
"created_at": "2016-05-10T00:16:54.469Z",
"post_type": "question",
"post_title": "qwerty",
"post_text": "asdasd asda sdasd",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": null,
"is_accepted": null,
"acceptor_id": null,
"answers": [
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 17
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 14
}
]
}
],
"comments": [
{
"id": 1,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 10
}
]
}
]
我的查询是:
SELECT
q.*,
json_agg(ac.*) AS answers,
json_agg(c.*) AS comments --comments on posts of post_id questions
FROM posts q
LEFT JOIN
(
SELECT
a.*,
json_agg(c.*) AS comments -- comments on posts of post_id answers
FROM posts a
LEFT JOIN comments c
ON a.post_id = c.parent_post_id
GROUP BY a.post_id
) ac
ON q.post_id = ac.parent_post_id
LEFT JOIN comments c
ON q.post_id = c.parent_post_id
WHERE q.post_id = 10
GROUP BY q.post_id
我得到的:
[
{
"post_id": "10",
"created_at": "2016-05-10T00:16:54.469Z",
"post_type": "question",
"post_title": "qwerty",
"post_text": "asdasd asda sdasd",
"post_author_id": 1,
"parent_post_id": null,
"is_accepted": null,
"acceptor_id": null,
"answers": [
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
}
],
"comments": [
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
}
]
}
]
最佳答案
一旦所有参与方都加入,分组就会发生,因此聚合将取决于生成的基数。加入带有答案和评论的帖子会导致它们之间的完全连接,从而复制所有值。它们需要分开并单独执行,您可以采用以下一种方法:
SELECT
q.*,
(SELECT json_agg(ac.*)
FROM (
SELECT a.*, json_agg(c.*) AS comments
FROM posts a
LEFT JOIN comments c ON (a.post_id = c.parent_post_id)
WHERE a.parent_post_id = q.post_id
GROUP BY a.post_id
) ac
) AS answers,
json_agg(c.*) AS comments --comments on posts of post_id questions
FROM posts q
LEFT JOIN comments c ON (q.post_id = c.parent_post_id)
WHERE q.post_id = 10
GROUP BY q.post_id;
或者:
SELECT q.*, qa.answers, qc.comments
FROM posts q
LEFT JOIN (
SELECT ac.parent_post_id, json_agg(ac.*) AS answers
FROM (
SELECT ac.*, json_agg(c.*) AS comments
FROM posts ac
LEFT JOIN comments c ON (c.parent_post_id = ac.post_id)
GROUP BY ac.post_id
) ac
GROUP BY ac.parent_post_id
) qa ON (qa.parent_post_id = q.post_id)
LEFT JOIN (
SELECT c.parent_post_id, json_agg(c.*) AS comments
FROM comments c
GROUP BY c.parent_post_id
) qc ON (qc.parent_post_id = q.post_id)
WHERE q.post_id = 10;
关于sql - SQL JOIN 查询中的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151721/
我正在测试设置SQLAlchemy以映射现有数据库。这个数据库是很久以前自动建立的,它是由我们不再使用的先前的第三方应用程序创建的,因此 undefined 某些预期的事情,例如外键约束。该软件将管理
这个问题在这里已经有了答案: What is the difference between "INNER JOIN" and "OUTER JOIN"? (28 个答案) 关闭 7 年前。 INNE
这个问题在这里已经有了答案: What is the difference between "INNER JOIN" and "OUTER JOIN"? (29 个回答) 关闭7年前. INNER J
假设有两个表: table1.c1 table1.c2 1 1 A 2 1 B 3 1 C 4 2
假设有两个表: table1.c1 table1.c2 1 1 A 2 1 B 3 1 C 4 2
一.先看一些最简单的例子 例子 Table A aid adate 1 a1 2&nb
数据库操作语句 7. 外连接——交叉查询 7.1 查询 7.2 等值连接 7.3 右外
我有两个表 'users' 和 'lms_users' class LmsUser belongs_to :user end class User has_one :lms_user
我试图避免在 Rails 中对我的 joins 进行字符串插值,因为我注意到将查询器链接在一起时灵活性会降低。 也就是说,我觉得 joins(:table1) 比 joins('inner join
我有这个代码 User.find(:all, :limit => 10, :joins => :user_points, :select => "users.*, co
我刚刚开始探索 Symfony2,我很惊讶它拥有如此多的强大功能。我开始做博客教程在: http://tutorial.symblog.co.uk/ 但使用的是 2.1 版而不是 2.0 我的问题是我
什么是 SQL JOIN什么是不同的类型? 最佳答案 插图来自 W3schools : 关于SQL JOIN 和不同类型的 JOIN,我们在Stack Overflow上找到一个类似的问题: http
我有两个 Hive 表,我正在尝试加入它们。这些表没有被任何字段聚集或分区。尽管表包含公共(public)键字段的记录,但连接查询始终返回 0 条记录。所有数据类型都是“字符串”数据类型。 连接查询很
我正在使用 Solr 的(4.0.0-beta)连接功能来查询包含具有父/子关系的文档的索引。连接查询效果很好,但我只能在搜索结果中获得父文档。我相信这是预期的行为。 但是,是否有可能在搜索结果中同时
我正在使用可用的指南/api/书籍自学 Rails,但我无法理解通过三种方式/嵌套 has_many :through 关联进行的连接。 我有用户与组相关联:通过成员(member)资格。 我在多对多
什么是 SQL JOIN,有哪些不同的类型? 最佳答案 插图来自 W3schools : 关于SQL JOIN 和不同类型的 JOIN,我们在Stack Overflow上找到一个类似的问题: htt
我正在尝试访问数据库的两个表。在商店里,我保留了一个事件列表,其中包含 Table Event id, name,datei,houri, dateF,Hourf ,capacity, age ,de
我有 4 个表:booking、address、search_address 和 search_address_log 表:(相关列) 预订:(pickup_address_id, dropoff_a
我在YML中有以下结构:。我正试着创造一个这样的结构:。作业名称和脚本用~分隔,作业用;分隔。。我可以使用以下命令使其正常工作。然而,我想知道是否可以用一个yq表达式来完成,而不是通过管道再次使用yq
我在YML中有以下结构:。我正试着创造一个这样的结构:。作业名称和脚本用~分隔,作业用;分隔。。我可以使用以下命令使其正常工作。然而,我想知道是否可以用一个yq表达式来完成,而不是通过管道再次使用yq
我是一名优秀的程序员,十分优秀!