2014-02-14 79 views
1

我有目前的兩個表。我想在一個查詢中檢索所有相應的訂單行。MySQL GROUP BY:如何返回多行?

enter image description here

我當前的代碼如下:

$query = ' 
SELECT * FROM table_a 
JOIN table_b ON table_b.order_id = table_a.order_id 
WHERE table_a.order_id = '1' 
GROUP BY table_a.order_id 
'; 

$result_prepare = $this->DB->prepare($query); 
$result_prepare->execute(); 

$result = $result_prepare->fetchAll(PDO::FETCH_ASSOC); 
$query_result = array('result'=>$result); 

print_r($query_result); 

輸出結果只返回第一行:

Array 
(
    [0] => Array 
     (
      [order_id] => 1 
      [row_id] => 1 
      [value] => 100 
     ) 

} 

我想輸出結果返回分組的所有行按行。

Array 
(
    [0] => Array 
     (
      [order_id] => 1 
      [rows] => Array 
        (
         [0] => Array 
          (
           [row_id] => 1 
           [value] => 100 

          ) 

         [1] => Array 
          (
           [row_id] => 2 
           [value] => 101 

          ) 

        ) 

     ) 

} 

我該如何做到這一點?我需要更改我的SQL查詢。

+1

你不能在sql中有這樣的輸出。您將始終擁有每行具有相同數量列的行。一列中不能有多個值。 –

+0

@VolkanUlukut是正確的 - SQL查詢將爲每一行返回3列,並且您的PHP顯示邏輯負責只在更改時打印'order_id'。 SQL本身就像SELECT SELECT order_id,row_id,value FROM TableB那樣簡單ORDER BY order_id,row_id' –

+0

請注意SQL用於關係集邏輯,而不是程序/數組。嘗試在結果集表單中寫出你想要的輸出。你的問題很混亂,我不明白你想要什麼(聚合,獨特,加入,?) – jean

回答

2

你的單行是MySQL處理GROUP BY沒有任何聚合函數如COUNT(),SUM(),MIN(),MAX()的結果。它將組合拆分爲單行,併爲不分組的列賦予不確定的值,並且不是可以依賴的。

雖然你不需要GROUP BY如果你想要一個索引爲order_id的數組結構。相反,執行一個簡單的查詢,然後在您的PHP代碼中,將其重構爲您正在查找的格式的循環。

// join in table_a if you need more columns.. 
// but this query is doable with table_b alone 
$query = " 
SELECT 
order_id, 
row_id, 
value 
FROM table_b 
WHERE order_id = '1' 
"; 

// You do not actually need to prepare/execute this unless you 
// are eventually going to pass order_id as a parameter 
// You could just use `$this->DB->query($query) for the 
// simple query without user input 
$result_prepare = $this->DB->prepare($query); 
$result_prepare->execute(); 

$result = $result_prepare->fetchAll(PDO::FETCH_ASSOC); 

// $result now is an array with order_id, row_id, value 
// Loop over it to build the array you want 
$final_output = array(); 
foreach ($result as $row) { 
    $order_id = $row['order_id']; 

    // Init sub-array for order_id if not already initialized 
    if (!isset($final_output[$order_id])) { 
    $final_output[$order_id] = array(); 
    } 
    // Append the row to the sub-array 
    $final_output[$order_id][] = array(
    'row_id' => $row['row_id'], 
    'value' => $row['value'] 
); 
    // You could just append [] = $row, but you would still 
    // have the order_id in the sub-array then. You could just 
    // ignore it. That simplifies to: 
    // $final_output[$order_id][] = $row; 
} 

// Format a little different than your output, order_id as array keys 
// without a string key called order_id 
print_r($final_output); 
+0

謝謝Michael! –

0

我想你想要這樣的:

$query = ' 
SELECT * 
FROM table_a 
JOIN table_b 
ON table_b.order_id = table_a.order_id 
WHERE table_a.order_id = '1' 
ORDER BY b.row_id 
'; 
0

我認爲你需要使用一個OUTER JOIN statment,而不是GROUP BY。