需要合并字段并获取唯一行

时间:2013-10-31 09:12:41

标签: mysql group-by group-concat

我有一个包含+1百万行的数据库,其结构如下:

CREATE TABLE IF NOT EXISTS `Performance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `CIDs` varchar(100) DEFAULT NULL,
  `COLOR` varchar(100) DEFAULT NULL,
  `Name` varchar(255) DEFAULT NULL,
  `XT` bigint(16) DEFAULT NULL,
  `MP` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `CIDs` (`CIDs`),
  KEY `COLOR` (`COLOR`),
  KEY `Name` (`Name`),
  KEY `XT` (`XT`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;

INSERT into `Performance` (`id`, `CIDs`, `COLOR`, `Name`, `XT`, `MP`) 
VALUES
(1, '1253374160', 'test test test test test', 'Load1', '89421331221', ''),
(2, '1271672029', NULL, 'Load1', '19421331221', NULL),
(3, '1188959688', NULL, 'Load2', '39421331221', NULL),
(4, '1271672029', NULL, 'Load3', '49421341221', 'Description'),
(5, '1271888888', NULL, 'Load4', '59421331221', 'Description');

输出应如下所示:

+----+------------+--------------------------+-------------+-------------+-------+-----------+---------+
| id | CIDs       | COLOR                    | XT          | MP          | Name  | PIDs      | unqName |
+----+------------+--------------------------+-------------+-------------+-------+-----------+---------+
|  1 | 1253374160 | test test test test test | 89421331221 |             | Load1 | 1,2       | Load1   |
|  3 | 1188959688 | NULL                     | 39421331221 | NULL        | Load2 | 3         | Load2   |
|  4 | 1271672029 | NULL                     | 49421341221 | Description | Load3 | 4,5       | Load3   |
+----+------------+--------------------------+-------------+-------------+-------+-----------+---------+

我怎么能尽快做到这一点? 我尝试了一些分组,但需要一些分钟:/

//编辑: 对于group by的解决方案,我需要4个子查询:/

// EDIT2: 按要求:

    SELECT id, 
       cids, 
       color, 
       xt, 
       mp, 
       name, 
       Concat(pids, ",", Group_concat(DISTINCT id)) AS PIDs, 
       Ifnull(name, id)                             AS unqName 
FROM   (SELECT id, 
               cids, 
               color, 
               xt, 
               mp, 
               name, 
               Concat(pids, ",", Group_concat(DISTINCT id)) AS PIDs, 
               Ifnull(mp, id)                               AS unqMP 
        FROM   (SELECT id, 
                       cids, 
                       color, 
                       xt, 
                       mp, 
                       name, 
                       Concat(pids, ",", Group_concat(DISTINCT id)) AS PIDs, 
                       Ifnull(xt, id)                               AS unqXT 
                FROM   (SELECT id, 
                               cids, 
                               color, 
                               xt, 
                               mp, 
                               name, 
                               Group_concat(DISTINCT id) AS PIDs, 
                               Ifnull(color, id)         AS unqCOLOR 
                        FROM   performance 
                        GROUP  BY unqcolor) m 
                GROUP  BY unqxt) x 
        GROUP  BY unqmp) y 
GROUP  BY unqname

0 个答案:

没有答案