-1

我正在尝试锻炼如何查询我拥有的表并在我的选择语句中生成特定的输出格式。源表数据如下

+-----------+------------------------+
| prefix_id | Description  | A  | B  |
+-----------+------------------------+
|     55207 | TEST 1       | 1  | 0  |
|     55363 | Test 2       | 1  | 0  |
|     55378 | Test 3       | 0  | 1  |
|     55379 | Test 4       | 1  | 1  |
+-----------+------------------------+

我想要的上述数据的输出如下

+-----------+------------+
| A         | B          |
+-----------+------------|
| TEST 1    | Test 3     |
| Test 2    | Test 4     |
| Test 4    | NULL       |
+-----------+------------+

如您所见,测试 4 描述出现两次,因为对于 A 列和 B 列都是如此,但顺序并不重要。NULL 字符只应出现在 A 或 B 列的末尾。只要每个条目在相应的列下出现一次,ID 并不重要。

也许临时表会有所帮助,但我不知道如何。列 A 或 B 的单独查询很容易,但将它们合并到输出中是我的问题。

将输出想象为您在 excel 中看到的内容,您希望列顶部的数据用底部的空白填充

非常感谢您的帮助。

笔记。希望在 sql 查询中实现这一点。查询输出使用名为 myDBR 的分析报告工具呈现。

4

2 回答 2

1

您可以在 SQL 中执行此操作。您要做的是对齐两个列表-幸运的是,您不关心顺序(因为您没有指定顺序的列)。

下面将数据分成两部分,一份用于“A”列,一份用于“B”列。然后它使用 MySQL 技巧来计算序列号(其他数据库将row_number()用于此)。

这是查询:

select MAX(A), MAX(B)
from ((select @rn1 := @rn1 + 1 as rn, Description as A, NULL as B
       from t cross join (select @rn1 := 0) const
       where A = 1
      ) union all
      (select @rn2 := @rn2 + 1 as rn, NULL as A, Description as B
       from t cross join (select @rn2 := 0) const
       where B = 1
      )
     ) t
group by rn
于 2013-05-02T18:33:47.703 回答
0

A simple database query can grab all results. Loop through them to determine whether they belong in column A, or B, or both.

<?php
// Query all entries from database
$rs = mysql_query('SELECT Description, A, B FROM table ORDER BY Description ASC');

// Loop through all rows in result set
while ( $row = mysql_fetch_assoc( $rs ) ){
    // if Column A = 1, add to $colA array
    if ( $row['A'] > 0 ) $colA[] = $row;
    // if Column B = 1, add to $colB array
    if ( $row['B'] > 0 ) $colB[] = $row;
}

Now you need to do something with the data. Hard to tell from your question if you want it displayed in HTML format, or dumped into CSV format, or something else.

But, you now have array $colA that contains rows matching A=1, and array $colB that contains rows matching B=1.

Here's an approach to output in HTML:

<?php

// Setup the base table
$table_html = '<table><tr><th>A</th><th>B</th></tr>';
$x=0;


// Determine which array has more children
if ( count( $colA ) >= count( $colB ) ) {
    $use = 'colA'; // A has more
    $counter = 'colB';
} else {
    $use = 'colB'; // B has more
    $counter = 'colA';
}

// The variable variable lets us use either $colA or $colB, as determined by the value in $use.
// More info here: http://php.net/manual/en/language.variables.variable.php
foreach ( $$use as $row ){
    // Append the current row, and if its counterpart exists, the other row

    if ( $use == 'colA' ){
        $table_html .= '<tr><td>' . $row['Description'] . '</td><td>' . ( isset( ${$counter}[ $x ] ) ? ${$counter}[ $x ]['Description'] : '' ) . '</td></tr>';
    } else {
        $table_html .= '<tr><td>' . ( isset( ${$counter}[ $x ] ) ? ${$counter}[ $x ]['Description'] : '' ) . '</td><td>' . $row['Description'] . '</td></tr>';
    }
    $x++; // Increment the counter
}

// Close the table
$table_html .= '</table>';

// Output to browser
echo $table_html;
于 2013-05-02T16:57:16.013 回答