如何删除重复结果并合并在 mySQL 查询中具有相似属性的对象?

Posted

技术标签:

【中文标题】如何删除重复结果并合并在 mySQL 查询中具有相似属性的对象?【英文标题】:How can I remove duplicate results and merge objects that have a similar property on a mySQL query? 【发布时间】:2017-08-13 06:44:18 【问题描述】:

我正在使用带有 node.js 的 mysql 库来创建这个查询:

SELECT vols.id_vol, vols.id_place, vols.id_vol_type, vols.id_user_creator, vols.name, vols.desc, vols.date_creation, vols.date_begin, vols.date_end, vols.duration, ' +
            ' vols.active, vols.start_time, vols.end_time, vols.insurance, vols.deleted, users.id_user, users.login, users.verified, users.photo_url, comments.message, comments.id_user, place.id_place, place.name AS placeName, place.lat, place.long' +
            ' FROM vols INNER JOIN users ON vols.id_user_creator = users.id_user INNER JOIN place ON vols.id_place = place.id_place LEFT JOIN comments ON comments.id_vol = vols.id_vol WHERE vols.deleted = 0

返回这个:

  "success": true,
  "vols": [
    
      "vol": 
        "id_vol": 1,
        "id_place": 1,
        "id_vol_type": 2,
        "id_user_creator": 1,
      ,
      "users": 
        "id_user": 1,
        "login": "",
        "verified": 0,
      ,
      "comments": 
        "message": "Muito fixe",
        "id_user": 3
      ,

      "vol": 
        "id_vol": 1,
        "id_place": 1,
        "id_vol_type": 2,
        "id_user_creator": 1,
      ,
      "users": 
        "id_user": 1,
        "login": "",
        "verified": 0,
      ,
      "comments": 
        "message": "ola",
        "id_user": 3
      

当前,它正在为具有该特定 vol ID 的每条评论重复“vol”。是否可以停止显示重复项并将 cmets 合并到单个数组上?

【问题讨论】:

【参考方案1】:

这是不可能的,我的意思是你可以用 group_concat 函数做一些事情,但是会很麻烦。

在类似情况下我通常会做的是:

    查询出所有相关卷 提取您要查看的所有用户ID、地点ID、评论ID。(在运行所有结果以提取此数据时,最好为每个结果创建一个对象,并将它们放入索引数组中(对象)对于每个可索引参数“用户、地点、评论”) 进行 3 个单独的查询,每个查询一个(用户、地点、cmets),您只选择相关元素(使用 WHERE id IN (1,5,7,8,10) 过滤器) 遍历每个查询的结果,并使用索引数组(对象)将它们插入到 vol 对象中

javascript 中类似于(抱歉不知道 node.js,所以我没有一个精确的解决方案

var results = db.queryResults('SELECT vols.id_vol, vols.id_place, vols.id_vol_type, vols.id_user_creator, vols.name, vols.desc, vols.date_creation, vols.date_begin, vols.date_end, vols.duration, vols.active, vols.start_time, vols.end_time, vols.insurance, vols.deleted FROM vols WHERE vols.deleted = 0');
var lookupByUserID = ;
var lookupByPlaceID = ;
var lookupByVolID = ;
var userIN = [];
var placeIN = [];
var volIN = [];
for (var i = 0; i < results.length; i++)

   var result = results[i];
   if (!lookupByUserID[result.id_user_creator])
       lookupByUserID[result.id_user_creator] = [];
   lookupByUserID[result.id_user_creator].push(result);

   if(!lookupByPlaceID[result.id_place])
      lookupByPlaceID[result.id_place] = [];
   lookupByPlaceID[result.id_place].push(result);

   lookupByVolID[result.id_vol] = result;
   userIN.push(result.id_user_creator);
   placeIN.push(result.id_place);
   volIN.push(result.id_vol);
   results.comments = [];


var userResults = db.queryResults('SELECT users.id_user, users.login, users.verified, users.photo_url FROM users WHERE id_user IN ('+userIN.join(',')+')');
var placeResults = db.queryResults('SELECT place.id_place, place.name AS placeName, place.lat, place.long FROM place WHERE id_place IN ('+placeIN.join(',')+')');
var commentResults = db.queryResults('SELECT comments.id_vol, comments.message, comments.id_user FROM comments WHERE id_vol IN ('+volIN.join(',')+')');
for (var i = 0; i < userResults.length; i++)

   var user = userResults[i];
    for (var j = 0; j < lookupByUserID[user.id_user].length; j++)
        lookupByUserID[user.id_user][j].created_by_user = user;

for (var i = 0; i < placeResults.length; i++)

   var place = placeResults[i];
    for (var j = 0; j < lookupByPlaceID[place.id_place].length; j++)
        lookupByPlaceID[place.id_place][j].place = place;

for (var i = 0; i < commentResults.length; i++)

    var comment = commentResults[i];
    lookupByVolID[comment.id_vol].comments.push(comment);

现在对象“结果”应该包含所有信息

【讨论】:

以上是关于如何删除重复结果并合并在 mySQL 查询中具有相似属性的对象?的主要内容,如果未能解决你的问题,请参考以下文章

如何合并两个select mysql查询的结果[重复]

mysql 将两个SQL语句查询结果并在一起

MySQL/PHP 输出数组重复查询然后显示结果,如何删除查询? [复制]

Solr的查询结果去重

MYSQL过滤表中某几个字段重复的数据

如何在 MySQL 的单个列中删除重复的逗号分隔值