2014-02-11 136 views
2

這是我的數據庫表,我如何查詢,以便我可以得到如下所需的輸出。我可以查詢數據庫使用單個查詢語句來獲取JSON輸出?Mysql查詢加入條件

Table name: rowManager 
    +-------------+-----------------+----------------+ 
    | rowid  |  rowname | showid   | 
    +-------------+-----------------+----------------+ 
    |   1 | first   |    0 | 
    |   17 | second   |    2 | 
    |   18 | third   |    0 | 
    |   20 | forth   |    0 | 
    +-------------+-----------------+----------------+ 
    Table name: row_vid 
    +-------------+----------------+-----------------+-----------------+ 
    | rowid  | name   |  description | submission_date | 
    +-------------+----------------+-----------------+-----------------+ 
    |   1 | Learn PHP  | abavavaav  | 2007-05-24  | 
    |   17 | Learn MySQL | sdasdsa   | 2007-05-24  | 
    |   20 | JAVA Script | Sanjay   | 2007-05-06  | 
    |   1 | JAVA   | Sanjay   | 2007-05-06  | 
    |   18 | Android  | Sanjay   | 2007-05-06  | 
    |   17 | ios   | Sanjay   | 2007-05-06  | 
    |   1 | python   | Sanjay   | 2007-05-06  | 
    |   18 | c++   | Sanjay   | 2007-05-06  | 
    |   18 | c#    | Sanjay   | 2007-05-06  | 
    |   17 | ruby   | Sanjay   | 2007-05-06  | 
    |   18 | JQuery   | Sanjay   | 2007-05-06  | 
    |   17 | objective c | Sanjay   | 2007-05-06  | 
    |   1 | JAVA Tutorial | Sanjay   | 2007-05-06  | 
    +-------------+----------------+-----------------+-----------------+ 



$rowQuery='SELECT * from row_vid,rowManagr where rowManagr.showid=0'; 

我試圖查詢這些表,使得它給了我像輸出:

{ 
    "responses":{ 
     "First":[ 
     { 
      "name":"Learn PHP" 

     }, 
     { 
      "name":"JAVA" 

     }, 
     { 
      "name":"python" 

     }, 
     { 
      "name":"JAVA Tutorial" 
     } 
     ], 
     "Other Show":[ 
     { 
      "name":"Learn MySQL" 
     }, 
     { 
      "name":"ios" 
     }, 
     { 
      "name":"ruby", 

     }, 
     { 
      "name":"objective c" 
     } 
     ], 
     "Videos":[ 
     { 
      "name":"Android" 
     }, 
     { 
      "name":"c++ " 
     }, 
     { 
      "name":"c#", 

     }, 
     { 
      "name":"JQuery" 
     } 
     ] 
    } 
} 

感謝

+1

我覺得你首先需要看看連接。 – Strawberry

+0

我想但不能得到所需的輸出:( – user3294288

回答

3

要開始,我相信這是你要找的查詢:

SELECT rowManager.rowname as category, row_vid.name as name 
FROM  rowManager, row_vid 
WHERE rowManager.rowid=row_vid.rowid 
ORDER BY rowManager.rowid, row_vid.name 

在PHP中,你可以這樣做:

$sql = "SELECT rowManager.rowname as category, row_vid.name as name FROM rowManager, row_vid WHERE rowManager.rowid=row_vid.rowid ORDER BY rowManager.rowid, row_vid.name" 

$query_result = mysqli_query($sql); 

// Piece together the $responses array, which will have [category] 
// as key, while value is an array of 'name':[name]' pairs. 
$responses = array(); 
while ($row = mysqli_fetch_assoc($query_result)) { 
    if (!array_key_exists($row['category'], $responses)) { 
    $responses[$row['category']] = array(); 
    } 
    array_push($responses[$row['category']], array('name' => $row['name'])); 
} 

// Entire $response array is the value of 'responses' key in the 
// final $results array. 
$results = array('responses' => $responses); 

// Convert $results_array to JSON and print. 
print(json_encode($results)); 
+0

謝謝............ :) – user3294288

1

你可能會想使用JOIN構建您收集的數據。但是,然後,你將不得不「手動」把我變成JSONmysql沒有內置函數返回JSON數據。您可以在php的響應中執行此操作,也可以使用CONCAT或類似命令在mysql中構建您的JSON響應。

你最好的選擇,以便能夠調試,跟蹤誤差和容易地通過遍歷您mysql結果,構建多維array具有相似的佈局,然後在運行json_encode處理您JSON佈局可能會在php做到這一點陣列。

0

如果你有rowManager與列名row_vid之間的關係ROWID,那麼你不能得到輸出,這些查詢 -

SELECT 
    * 
FROM 
    row_vid AS rvid 
     LEFT JOIN 
    rowManagr rm ON rvid.rowid = rm.rowid 
WHERE 
    rm.showid = 0 

這個查詢後,你將不得不使用PHP來遍歷這個查詢結果和格式化您的預期數據並使用PHP將這些數據編碼爲json。