2013-08-19 61 views
0

我有兩個mysql數據庫表,一個是針對posts和other的註釋。從兩個mysql表中獲取數據,其中一個表的行連接到其他許多

郵政表

+----+-------+ 
| ID | texts | 
+----+-------+ 
| 1 | abc | 
| 2 | xyz | 
+----+-------+ 

和意見表

+----+--------+-------+ 
| ID | postid | texts | 
+----+--------+-------+ 
| 1 | 1  | abc1 | 
| 2 | 1  | abc2 | 
| 3 | 1  | abc3 | 
| 4 | 2  | xyz1 | 
| 5 | 2  | xyz2 | 
+----+--------+-------+ 

現在,如何獲得具有最低限度的MySQL查詢請求的帖子,使輸出類似,

$data = array(
    0 => array(
     ID => 1, 
     texts => abc, 
     comments => array(
      0 => array(
       ID => 1, 
       texts => abc1 
      ) 
      1 => array(
       ID => 2, 
       texts => abc2 
      ) 
      2 => array(
       ID => 3, 
       texts => abc3 
      ) 
     ) 
    ) 
    1 => array(
     ID => 2, 
     texts => xyz, 
     comments => array(
      0 => array(
       ID => 4, 
       texts => xyz1 
      ) 
      1 => array(
       ID => 5, 
       texts => xyz2 
      ) 
     ) 
    ) 
) 
+0

如果你有一個大表查詢將是緩慢和PHP擁有大內存因爲陣列的使用...這不是一個非常聰明的想法。 –

+0

@RaymondNijland然後如何去做而不用查詢。 – user1995997

+0

你的帖子表會增長...真正的你需要一個查詢,但你爲什麼要獲取完整的表..多數民衆贊成在不好。 –

回答

1

這將是有益的,如果你能提供的代碼把結果陣列

讓我先推薦一個更好的多維數組,這將是更容易使用。

Array格式:

$data = array(
    post.ID => array(
     "texts" => post.texts, 
     "comments" => array(
      comments.ID => comments.texts, 
     ), 
    ), 
); 

上述格式將是比較容易的工作特別是對直接訪問到陣列中,也爲foreach循環。

現在分配從MySQL中的數據結果轉換成使用mysqli_*功能的陣列和while循環請執行以下操作:

//connect to mysql database 
$link = $mysqli_connect("localhost","your_user","your_password","your_database"); 
//form mysql query 
$query = " 
    SELECT 
     post.ID AS post_id, 
     post.texts AS post_texts, 
     comments.ID AS comments_id, 
     comments.texts AS comments_texts 
    FROM 
     post 
     LEFT JOIN comments ON (comments.postid = post.ID) 
    WHERE 
     posts.ID < 10 
"; 
//run mysql query and return results 
$mysqli_result = mysqli_query($link,$query); 
//define empty $data array 
$data = array(); 
//loop through result sets fetching string array with each result row 
while($row = mysqli_fetch_array($mysqli_result)){ 
    //set the post text if not already set 
    if(!isset($data[$row["post_id"]]["texts"])){ 
     $data[$row["post_id"]]["texts"] = $row["post_texts"]; 
    } 
    //set the comments data if not NULL otherwise set comments to empty array to maintain structure 
    if(!empty($row["comments_id"])){ 
     $data[$row["post_id"]]["comments"][$row["comments_id"]] = $row["comments_texts"]; 
    } else { 
     $data[$row["post_id"]]["comments"] = array(); 
    } 
} 
//free the results set 
mysqli_free_result($mysqli_result); 
//close connection to mysql database 
mysqli_close($link); 

//print out the post text with the id of 1 with two line breaks 
//be careful using this method unless you are sure that post with id of 1 exists or first check if(isset($data["1"])){...} 
print $data["1"]["texts"]."<br /><br />"; 

//loop through all of the comments for a particular post with id of 1 
foreach($data["1"]["comments"] as $key => $value){ 
    //print out the comment id with a line break 
    print "Comment ID: ".$key."<br />"; 
    //print out the comments texts with two line breaks 
    print "Comment: ".$value."<br /><br />"; 
} 

//loop through and print all the post texts and how many comments exist for the post 
foreach($data as $key => $value){ 
    //print the post ID with a line break 
    print "Post ID: ".$key."<br />"; 
    //print the post texts with a line break 
    print "Post: ".$value["texts"]."<br />"; 
    //count the number of comments 
    $num_comments = count($value["comments"]); 
    //get correct plural form of noun 
    ($num_comments==1) ? $comments = "comment" : $comments = "comments"; 
    //print the number of comments for the post with two line breaks 
    print $num_comments." ".$comments." for this post.<br /><br />"; 
} 
2

如何約

SELECT * 
FROM Post p LEFT JOIN 
     Comments c ON p.ID = c.postID 
+0

謝謝:)它的工作原理。如果我想選擇特定的帖子怎麼辦?即'posts.ID <10' – user1995997

+0

只需添加一個WHERE子句即可。像WHERE posts.ID <10 –

+0

@ user1995997這是否回答你的問題?或者你需要幫助,把結果放入數組中,就像你的問題中的'$ data'一樣? – amaster

相關問題