2013-05-02 55 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