如何将MYSQL中的重复元素转换为数组?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将MYSQL中的重复元素转换为数组?相关的知识,希望对你有一定的参考价值。

目前,我有以下查询:

SELECT c.Permlink
     , c.ParentID
     , c.AuthorID
     , u.Username AS Author
     , c.Title
     , c.Body
     , c.Metadata
     , c.PostTime
     , v.VoterID
     , v.VoteValue
     , voterID.Username AS Voter
  FROM comments c
  LEFT 
  JOIN deleted_comments d
    ON c.Permlink = d.Permlink
  LEFT 
 JOIN votes v
    ON c.Permlink = v.Permlink 
 JOIN users u
    ON u.id = c.AuthorID 
  LEFT 
  JOIN users As voterID 
    ON voterID.ID = v.VoterID
 WHERE c.AuthorID = 88077 
  AND c.ParentID IS NULL
  AND d.Permlink IS NULL

我得到以下结果:

[
    
        "Permlink" : 3,
        "ParentID" : null,
        "AuthorID" : 88077,
        "Author" : "rishi556",
        "Title" : "",
        "Body" : "foxon",
        "Metadata" : "",
        "PostTime" : 1589475759,
        "VoterID" : 88077,
        "VoteValue" : 1,
        "Voter" : "rishi556"
    ,
    
        "Permlink" : 5,
        "ParentID" : null,
        "AuthorID" : 88077,
        "Author" : "rishi556",
        "Title" : "",
        "Body" : "foxonier?",
        "Metadata" : "",
        "PostTime" : 1589475774,
        "VoterID" : 1278255,
        "VoteValue" : 1,
        "Voter" : "gamer556"
    ,
    
        "Permlink" : 5,
        "ParentID" : null,
        "AuthorID" : 88077,
        "Author" : "rishi556",
        "Title" : "",
        "Body" : "foxonier?",
        "Metadata" : "",
        "PostTime" : 1589475774,
        "VoterID" : 88077,
        "VoteValue" : 1,
        "Voter" : "rishi556"
    ,
    
        "Permlink" : 9,
        "ParentID" : null,
        "AuthorID" : 88077,
        "Author" : "rishi556",
        "Title" : "",
        "Body" : "does this work?",
        "Metadata" : "",
        "PostTime" : 1589475858,
        "VoterID" : null,
        "VoteValue" : null,
        "Voter" : null
    
]

[为了减少重复,我希望将VoteIDVoteValueVoter中的数据放入数组中,因此使用Permlink:5作为示例,如下所示:

[
    
        "Permlink" : 5,
        "ParentID" : null,
        "AuthorID" : 88077,
        "Author" : "rishi556",
        "Title" : "",
        "Body" : "foxonier?",
        "Metadata" : "",
        "PostTime" : 1589475774,
        "Votes" : ["VoterID" : 1278255,"VoteValue" : 1,"Voter" : "gamer556","VoterID" : 88077, "VoteValue" : 1,"Voter" : "rishi556" ]

    
]

对于那些票数为空(表示尚未收到票数的人),它应该只是一个空数组。

我需要在查询中添加哪些内容?还是无法更改查询,而我需要在代码中进行操作?

答案

好,所以我看到“数组表”是一个单独的表。这意味着您有2个选择来解决此问题。

1。在您的代码中(未经测试!!!!)

理想情况下,您可以将其分为两个查询(一个查询获得评论,一个查询获得投票)

SELECT c.Permlink
     , c.ParentID
     , c.AuthorID
     , u.Username AS Author
     , c.Title
     , c.Body
     , c.Metadata
     , c.PostTime
  FROM comments c
  LEFT 
  JOIN deleted_comments d
    ON c.Permlink = d.Permlink
 JOIN users u
    ON u.id = c.AuthorID 
  AND c.ParentID IS NULL
  AND d.Permlink IS NULL

然后在该查询中,遍历票表以获取票数

SELECT votes.VoterID, votes.VoteValue,(SELECT Users.username FROM users WHERE users.ID = votes.VoterID) FROM votes WHERE comments.Permlink = votes.Permlink

2。完全在My SQL

如果您必须在mysql中执行此操作或有充分的理由这样做,这是一个查询,它可以为您提供所需的确切信息(所有投票的JSON字符串)

SELECT
  comments.Permlink,
  comments.ParentID,
  comments.AuthorID,
  users.Username AS Author,
  comments.Title,
  comments.Body,
  comments.Metadata,
  comments.PostTime,
  CONCAT("[", 
    (SELECT GROUP_CONCAT(CONCAT('"voter_id":',votes.VoterID,', "voteValue": ',votes.VoteValue,',"voter_name":"',(SELECT Users.username FROM users WHERE users.ID = votes.VoterID), '"'))
  FROM votes WHERE
    comments.Permlink = votes.Permlink),"]") as vote_data
FROM
  comments
LEFT JOIN
  deleted_comments
ON
  comments.Permlink = deleted_comments.Permlink
JOIN
  users
ON
  users.id = comments.AuthorID
WHERE
  comments.AuthorID = 0 AND comments.ParentID IS NULL AND deleted_comments.Permlink IS NULL

以上是关于如何将MYSQL中的重复元素转换为数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何将字符串加载到 BYTE* 数组(C++)的元素中 [重复]

如何复制数组中不同次数的元素?

如何去掉一个数组的重复元素:数组去重

在java中将字符串数组元素转换为字节数组[重复]

如何将重复的 XML 元素序列化为数组?

PHP将带有一些(重复)元素的XML转换为Json到Json数组[重复]