我试图找出如何通过列名合并两个表,以便值将包含在一个唯一列中。在此示例中,数据完全分布在2个主表中。一个名为comments的表包含有关用户的所有信息,这些信息已写在文档上并在那里留下评论。第二个表包括以下注释的回复,其parentID直接链接到注释的commentID。我不确定是否真的可以用SQL解决这个问题。另一种方法是在R或Python中获取数据并将它们合并在一起。
使用以下查询,只能从两个表中选择多个列。
QUERY:
"SELECT username, commentID, DocumentID, null as ReplyID, nameID,
text, publishedAt FROM Comments
UNION ALL
SELECT username, ParentID as commentID, DocumentID, ReplyID, CommentID_ParentID,
nameID, text, publishedAt FROM Replies
ORDER BY IF(commentID=ParentID, commentID, ReplyID), publishedAt DESC
我还读到了递归查询,通过解决这样的结构可能会有所帮助。通常,目标是创建一个包含两个表中所有信息的表。
TABLE Comments >>
commentID DocumentID nameID username text publishedAt
aa11 yxc123 UBH7x Jacky WOW 2018-03-04 10:26:05
bb11 yxc123 UBH0k Jason Cool 2018-03-04 10:27:10
cc11 yxc123 XT3m5 Peter Amazing 2018-03-06 11:03:11
dd11 www789 B4TCD Karl Boring. 2018-03-04 10:11:31
ff11 www789 VYA32 Anonymous ZzZ.. 2018-03-05 13:11:34
... ... ... ... ... ...
TABLE Replies >>
ReplyID DocumentID nameID ParentID username text publishedAt
aa11.sew2 yxc123 HYSE2 aa11 Sven NO 2018-03-04 10:28:30
aa11.exb yxc123 UBH7x aa11 Jacky WHAT? 2018-03-04 10:28:40
cc11.8bh yxc123 kdggk cc11 Betty YES. 2018-03-06 11:11:05
cc11.qxv yxc123 lldsl cc11 Sarah ILIKE 2018-03-04 11:16:25
dd11.wrt www789 shads dd11 XYZ TRUE. 2018-03-05 10:16:05
dd11.xcj www789 B4TCD dd11 Karl thx. 2018-03-04 10:19:44
...
DESIRED RESULT
username commentID DocumentID ReplyID nameID text published
Jacky aa11 yxc123 NULL UBH7x WOW 2018-03-04 10:26:05
Sven aa11 yxc123 aa11.sew2 HYSE2 NO 2018-03-04 10:28:30
Jacky aa11 yxc123 aa11.exb UBH7x WHAT? 2018-03-04 10:28:40
Jason bb11 yxc123 NULL UBH0k Cool 2018-03-04 10:27:10
Peter cc11 yxc123 NULL XT3m5 Amazing 2018-03-06 11:03:11
Betty cc11 yxc123 cc11.8bh kdggk YES. 2018-03-06 11:11:05
Sarah cc11 yxc123 cc11.qxv lldsl ILIKE 2018-03-04 11:16:25
Karl dd11 www789 NULL B4TCD Boring. 2018-03-04 10:11:31
编辑:我用所需输出的不同大小修复了问题,但顺序仍然是错误的。正如您在当前输出中所看到的,不执行分层顺序。首先,将显示所有注释,之后将执行回复,如表格末尾所示:
Current Output
username commentID DocumentID ReplyID nameID text published
Jacky aa11 yxc123 NULL UBH7x WOW 2018-03-04 10:26:05
Jason bb11 yxc123 NULL UBH0k Cool 2018-03-04 10:27:10
Peter cc11 yxc123 NULL XT3m5 Amazing 2018-03-06 11:03:11
Karl dd11 www789 NULL B4TCD Boring. 2018-03-04 10:11:31
Sven aa11 yxc123 aa11.sew2 HYSE2 NO 2018-03-04 10:28:30
Jacky aa11 yxc123 aa11.exb UBH7x WHAT? 2018-03-04 10:28:40
Betty cc11 yxc123 cc11.8bh kdggk YES. 2018-03-06 11:11:05
Sarah cc11 yxc123 cc11.qxv lldsl ILIKE 2018-03-04 11:16:25