2012-06-17 89 views
1

所有變量賦值我有三個表:選擇從最新的5個帖子

  1. post(ID) - 具有所有條目
  2. tag(ID) - 擁有所有的標籤
  3. assign(POST_ID, TAG_ID) - 指定標籤的帖子,可有多行一個職位

這是我到目前爲止有:

$ sql_post = mysql_query(「SELECT title,content,time,web FROM post ORDER BY time DESC LIMIT 5」);

$ sql_tag = mysql_query(「SELECT tag.name,tag.id,post.id FROM assign LEFT JOIN tag ON assign.tag_id = tag.id LEFT JOIN post ON assign.post_id = post.id WHERE post.time >(SELECT MIN(time)FROM(SELECT time FROM FROM ORDER BY time DESC LIMIT 6)two)「);

我想將它全部變成一個大陣列的,所以這樣的事情(馬虎摘要):

array(
    post_id => array(
     "title" => "post's title...", 
     "content" => "post's content...", 
     "tag" => array(
      tag_id => "tag 1's name", 
      tag_id => "tag 2's name 

...然後使用的foreach對我的博客上顯示出來。

我需要幫助的是sql的$sql_tag。我如何製作它,以便爲最新的5個帖子選擇所有作業?它只爲帖子選擇一個標籤。

回答

0

爲什麼不循環你感興趣的帖子?

$sql_post = mysql_query("SELECT id, title,content,time,web FROM post ORDER BY time DESC LIMIT 5"); 

while ($row = mysql_fetch_assoc($sql_post)) { 
    $sql_tag = mysql_query("SELECT tag.name,post.id FROM assign LEFT JOIN tag ON assign.tag_id=tag.id WHERE assign.post_id='".$row['id']."'"); 
} 
0

嘗試:

SELECT 
    c.id, 
    c.name, 
    a.id 
FROM 
    (
     SELECT id 
     FROM post 
     ORDER BY time DESC 
     LIMIT 5 
    ) a 
LEFT JOIN 
    assign b ON a.id = b.post_id 
LEFT JOIN 
    tag c ON b.tag_id = c.id