2012-09-13 81 views
0

我有此代碼哪些匹配在MySQL的視圖稱爲vAlbums此返回查詢的JSON陣列導致PDO查詢另一個PDO查詢

function getAlbumPics($arno){ 
    $aSql = "SELECT albu_ablumid, albu_name_en, albu_name_de, albu_name_fr, albu_name_nl, albu_name_es, albu_name_it, albu_photourl FROM vAlbums WHERE site_arno=:arno"; 
    try { 
     $db = getConnection(); 
     $aStmt = $db->prepare($aSql); 
     $aStmt->bindParam(":arno",$arno); 
     $aStmt->execute(); 
     $albums = $aStmt->fetchAll(PDO::FETCH_OBJ); 
     $arrAID = $aStmt->fetchColumn(2); 
     $db = null; 
     echo '{"albums": ' . json_encode($albums) . '}'; 
    } catch(PDOException $e) { 
     echo '{"error":[{"text":"'. $e->getMessage() .'"}],'; 
     echo '"SQL": ' . json_encode($aSql) .'}'; 
    } 
} 

我需要做一個子查詢將一個陣列在陣列中的每個專輯像這樣

{ 
    "albums": [ 
     { 
      "albu_ablumid": "1", 
      "photos": [ 
       { 
        "photourl": "photo1" 
       }, 
       { 
        "photourl": "photo2" 
       }, 
       { 
        "photourl": "photo3" 
       } 
      ] 
     }, 
     { 
      "albu_ablumid": "2", 
      "photos": [ 
       { 
        "photourl": "photo1" 
       }, 
       { 
        "photourl": "photo2" 
       }, 
       { 
        "photourl": "photo3" 
       } 
      ] 
     } 
    ] 
} 

照片可有人告訴如何實現這一目標的MySQL查詢的照片是:

SELECT * FROM photos WHERE album_id = x 

謝謝

回答

1

爲什麼不在一個查詢中做一個連接和組concat?

SELECT 
    albu_ablumid, 
    albu_name_en, 
    albu_name_de, 
    albu_name_fr, 
    albu_name_nl, 
    albu_name_es, 
    albu_name_it, 
    albu_photourl, 
    group_concat(photourl) // Guessing at column Name 
FROM 
    vAlbums a 
     join photos b 
      on a.albu_ablumid=b.album_id 
WHERE 
    site_arno=:arno 
group by 
    albu_ablumid, 
    albu_name_en, 
    albu_name_de, 
    albu_name_fr, 
    albu_name_nl, 
    albu_name_es, 
    albu_name_it, 
    albu_photourl 

這將返回與專輯匹配的每行中以逗號分隔的字符串中的所有照片。然後,您可以根據需要在代碼中輕鬆分割它。它可以節省大量連接並運行多個查詢。

這裏是我的戲分貝一點例子來說明它是如何發揮作用:

mysql> select * from table1; 
+---------+------+------+-------------+ 
| autonum | ID | name | metavalue | 
+---------+------+------+-------------+ 
|  1 | 1 | Rose | Drinker  | 
|  2 | 1 | Rose | Nice Person | 
|  3 | 1 | Rose | Runner  | 
|  4 | 2 | Gary | Player  | 
|  5 | 2 | Gary | Funny  | 
|  6 | 2 | Gary | NULL  | 
|  7 | 2 | Gary | Smelly  | 
+---------+------+------+-------------+ 
7 rows in set (0.00 sec) 

mysql> select ID, group_concat(metavalue) from table1 group by ID; 
+------+----------------------------+ 
| ID | group_concat(metavalue) | 
+------+----------------------------+ 
| 1 | Drinker,Nice Person,Runner | 
| 2 | Player,Funny,Smelly  | 
+------+----------------------------+ 
2 rows in set (0.00 sec) 

,並用一些簡單的代碼,如果你需要照顧的NULL出於某種原因:

mysql> select ID, group_concat(coalesce(metavalue,'Empty')) from table1 group by ID; 
+------+-------------------------------------------+ 
| ID | group_concat(coalesce(metavalue,'Empty')) | 
+------+-------------------------------------------+ 
| 1 | Drinker,Nice Person,Runner    | 
| 2 | Player,Funny,Empty,Smelly     | 
+------+-------------------------------------------+ 
2 rows in set (0.00 sec) 
+0

這是一個好主意,我想看看如果PHP/PDO路由將氾濫,如果可能的話,謝謝@Fluffeh –

+0

剛試過這個,真棒謝謝 –