group_concat之后的数据unpivot list ids

时间:2017-03-17 05:50:56

标签: mysql sql

以下链接指向:Sqlfiddle

我的表格内容

mysql> select * from tb_dts;
+----+------+------+
| Id | key1 | key2 |
+----+------+------+
|  1 |    1 |    1 |
|  2 |    1 |    1 |
|  3 |    1 |    1 |
|  4 |    2 |    1 |
|  5 |    2 |    1 |
|  6 |    2 |    1 |
|  7 |    2 |    1 |
|  8 |    1 |    2 |
|  9 |    1 |    2 |
| 10 |    1 |    2 |
| 11 |    1 |    2 |
| 12 |    1 |    2 |
| 13 |    3 |    1 |
| 14 |    3 |    1 |
| 15 |    3 |    1 |
| 16 |    3 |    1 |
| 17 |    2 |    2 |
| 18 |    2 |    2 |
| 19 |    2 |    2 |
| 20 |    2 |    3 |
| 21 |    2 |    3 |
| 22 |    2 |    3 |
| 23 |    3 |    2 |
| 24 |    3 |    2 |
| 25 |    3 |    2 |
| 26 |    3 |    2 |
+----+------+------+
26 rows in set (0.00 sec)

这是我试过的

mysql> select group_concat(id) from tb_dts group by key1,key2 limit 0,4;
+------------------+
| group_concat(id) |
+------------------+
| 1,2,3            |
| 8,9,10,11,12     |
| 4,5,6,7          |
| 17,18,19         |
+------------------+
4 rows in set (0.00 sec)

mysql> select id from tb_dts group by key1,key2 limit 0,4;
+----+
| id |
+----+
|  1 |
|  8 |
|  4 |
| 17 |
+----+
4 rows in set (0.00 sec)

这是我期望得到group by key1,key2 limit 0,4;的输出:我想要一列中的所有ID,它都属于key1和key2的限制组,我不知道如何得到它group_concat无法帮助我

对于key1,key2所有id所有group by key1,key2 limit 0,4;的前4个唯一组合,我的问题是使用+----+ | id | +----+ | 1 | | 2 | | 3 | | 8 | | 9 | | 10 | | 11 | | 12 | | 4 | | 5 | | 6 | | 7 | | 17 | | 18 | | 19 | +----+ 15 rows in set (0.00 sec)

DROP TABLE IF EXISTS `tb_dts`;
CREATE TABLE `tb_dts` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `key1` int(11) DEFAULT '-99',
  `key2` int(11) DEFAULT '-99',
  PRIMARY KEY (`Id`),
  KEY `main` (`key1`,`key2`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;

LOCK TABLES `tb_dts` WRITE;
INSERT INTO `tb_dts` VALUES (1,1,1),(2,1,1),(3,1,1),(4,2,1),(5,2,1),(6,2,1),(7,2,1),(8,1,2),(9,1,2),(10,1,2),(11,1,2),(12,1,2),(13,3,1),(14,3,1),(15,3,1),(16,3,1),(17,2,2),(18,2,2),(19,2,2),(20,2,3),(21,2,3),(22,2,3),(23,3,2),(24,3,2),(25,3,2),(26,3,2);
UNLOCK TABLES;

表格结构

void nospace(char line[])
{
    for (int i = 0; i < sizeof(line) / sizeof(line[0]); i++)
    {
        if (line[i-1] == ' ' && line[i] == ' ')
        {
            line[i-1] = line[i];
            line[i] = line[i+1];
        }
    }

3 个答案:

答案 0 :(得分:0)

有办法解决问题。

首先,你必须创建一个像

这样的表
CREATE TABLE `Tmp` (
  `Id` int(11)
);

然后,在运行代码

之后
select @TmpStr = group_concat(id) from tb_dts group by key1,key2 limit 0,1;

--@TmpStr  = "1,2,3"

通过&#34;,#34;为Split @TmpStr写一个功能并分别插入Tmp。

并继续为

做同样的事情
select @TmpStr = group_concat(id) from tb_dts group by key1,key2 limit 1,1;
select @TmpStr = group_concat(id) from tb_dts group by key1,key2 limit 2,1;
select @TmpStr = group_concat(id) from tb_dts group by key1,key2 limit 3,1;

它会显示您想要的结果。

答案 1 :(得分:0)

使用下面的分隔符

 select group_concat(id SEPARATOR '\n') from tb_dts group by key1,key2

答案 2 :(得分:0)

如果订单无关紧要,请尝试:

请参阅此处的输出:SQL Fiddle

select id 
from tb_dts  
where (key1,key2) in (
       select key1,key2 
       from (
                select distinct key1,key2 from tb_dts limit 0,4
       ) as sub
);