简体   繁体   English

MySQL将多行(变量)分组为多列的一行

[英]MySQL group multiple(variable) rows into one row with multiple columns

I'm looking for a query that will return me several rows into columns but without knowing the number of rows beforehand. 我正在寻找一个查询,该查询将多行返回几列,但事先不知道行数。 I have searched and the only solutions I found involve knowing how many rows there are. 我进行了搜索,发现的唯一解决方案涉及知道有多少行。

Here's an example table: 这是一个示例表:

parentID    colA       colB
2           aaaaaa     1000.00
2           bbbbbb     1500.00
3           cccccc     500.00
3           dddddd     700.00
3           eeeeee     2000.00

and i need it to look like: 我需要它看起来像:

parentID    colA(n)      colB(n)     colA(n+1)     colB(n+1)     colA(n+2)     colB(n+2)
2           aaaaaaa      1000.00     bbbbbb        1500.00       NULL          NULL
3           cccccc       500.00      dddddd        700.00        eeeeee        2000.00

I realize this should be done in PHP but I need it to be in mysql for a third party excel exporter plugin I'm using. 我意识到这应该在PHP中完成,但我需要在mysql中使用我正在使用的第三方excel出口程序插件。

Edit: Is there a way to do this if I know the maximum number of columns I'll need? 编辑:如果我知道我需要的最大列数,有没有办法做到这一点?

You cannot do a query in SQL without knowing the number of columns. 如果不知道列数,则无法在SQL中进行查询。

The columns of a SELECT-list must be fixed at the time of parsing the query. SELECT列表的列必须在解析查询时固定。 What you're asking for is that the state of data, which is not known until the query executes, determines the number of columns. 您要的是查询执行之前未知的数据状态决定列数。 That is not the way SQL works. 那不是SQL的工作方式。

To accomplish a pivot-type operation, or any query where the data determines the columns, you have two choices: 要完成数据透视型操作或数据确定列的任何查询,您有两种选择:

  • Do a preparatory query to discover how many distinct groups you want to fetch, and use this to build the query with a matching number of columns. 进行预查询以发现要获取多少个不同的组,并使用此组构建具有匹配列数的查询。

  • Query all the data in rows, fetch it back into your application, and then transform the result set wholly within data structures (ie arrays) within your code. 查询行中的所有数据,将其取回应用程序,然后将结果集完全转换为代码中的数据结构(即数组)。

Either way, you need to write application code, either before or after fetching the data. 无论哪种方式,您都需要在获取数据之前或之后编写应用程序代码。


Re your comment: You're right, this isn't a traditional pivot, but it's similar in that data is driving the number of columns. 发表您的评论:没错,这不是传统做法,但在数据驱动列数方面类似。 That is, you need as many columns as 2x the number of rows in the largest group. 也就是说,您需要的列数是最大组中行数的2倍。 But they won't all be filled, because the number of rows per group varies. 但是它们不会全部填满,因为每个组的行数各不相同。 You don't have a fixed number of row per group, therefore you can't fill a fixed number of columns per group. 每个组没有固定数量的行,因此每个组不能填充固定数量的列。

I'd really recommend you use the latter strategy: fetch the data as rows, as they are stored in the database. 我真的建议您使用后一种策略:将数据存储在数据库中,以行的形式获取。 Then post-process it in your application code. 然后在您的应用程序代码中对其进行后处理。 Loop over the result set and build your data structure incrementally as you fetch rows from the database. 从数据库中获取行时,循环遍历结果集并逐步构建数据结构。

For example, in PHP: 例如,在PHP中:

while ($row = $stmt->fetch()) {
  $data[$row['parentID']][] = $row['colA'];
  $data[$row['parentID']][] = $row['colB'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM