2013-05-02 60 views
-1

我正在试着如何查询我拥有的表,并在我的select语句中生成特定的输出格式。源表数据如下MySQL表列到列表查询

+-----------+------------------------+ 
| 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真但顺序并不重要。空字符只应出现在列A或B的末尾。只要每个条目在相应的列下出现一次,这些ID就不重要。

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

想象输出为您在Excel中看到的东西,你想用空格填充出列的顶部数据在底部

你的帮助是非常赞赏。

注意。希望在sql查询中实现这一点。查询输出使用称为myDBR的分析报告工具进行渲染。

+0

A柱和B表达某种关系? – 2013-05-02 16:49:11

+0

扩展Mike的评论,是否有一个原因为什么test3与test1并列,而不是test2? – Strawberry 2013-05-02 16:50:53

+0

列A和列B之间没有关系。列A是所有描述,其中列A是真,列是列B为真的所有描述。这就像试图在一个sql查询中获得两个单独的列表。 – user1715656 2013-05-02 19:25:41

回答

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 
+0

看起来很有前途......我会尝试将其应用于我的真实世界场景 – user1715656 2013-05-02 19:29:59

0

简单的数据库查询可以获取所有结果。循环浏览它们以确定它们是属于A列还是B列还是两者。

<?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; 
} 

现在您需要对数据进行一些操作。很难告诉你的问题,如果你希望它以HTML格式显示,或倾倒成CSV格式,或其他东西。

但是,您现在拥有包含匹配A = 1的行的数组$ colA和包含匹配B = 1的行的数组$ colB。

下面是输出的方法在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; 
+0

也许我应该补充一点,我试图在sql查询中做到这一点 – user1715656 2013-05-02 19:27:36