Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询以解组数据_Mysql - Fatal编程技术网

MySQL查询以解组数据

MySQL查询以解组数据,mysql,Mysql,我的问题与相同,但是针对MySQL而不是SQL Server。可以用MySQL进行解组吗?不幸的是,MySQL没有“Unpivot”函数。以下是我需要的一个示例: 原始数据: ---------------------------------- owner id | name | occurances ---------------------------------- 1 | red | 4 1 | yellow | 2 1

我的问题与相同,但是针对MySQL而不是SQL Server。可以用MySQL进行解组吗?不幸的是,MySQL没有“Unpivot”函数。以下是我需要的一个示例:

原始数据:

----------------------------------
owner id |   name    | occurances
----------------------------------
1        |   red     | 4
1        |   yellow  | 2
1        |   green   | 3
----------------------------------
要输出的查询:

---------------
id |   name
---------------
1  |   red
1  |   red
1  |   red
1  |   red
1  |   yellow
1  |   yellow
1  |   green
1  |   green
1  |   green
---------------

这需要一组数字。这里有一个方法:

 select id, name
 from t join
       (select d1.d + 10 * d2.d + 100*d3.d as num
        from (select 1 as d union all select 2 union all select 3 union all
              select 4 union all select 5 union all select 6
              select 7 union all select 8 unin all select 9 union all select 0
             ) d1 cross join
              (select 1 as d union all select 2 union all select 3 union all
              select 4 union all select 5 union all select 6
              select 7 union all select 8 unin all select 9 union all select 0
             ) d2 cross join
              (select 1 as d union all select 2 union all select 3 union all
              select 4 union all select 5 union all select 6
              select 7 union all select 8 unin all select 9 union all select 0
             ) d3
        ) n
        where n.num between 1 and occurrences
这适用于999以下的数字