2012-07-13 120 views
1

我試圖做一個函數有點類似Facebook的通知窗口。他們的功能似乎結合了任何一種事件並在日期之後顯示出來。加入兩個表按日期排序並顯示結果

所以我有2個表格: 文章和評論。 都有2個相似的行:uniquepostowner和date_posted。

我想加入他們並一個接一個地顯示他們。 實施例:

User A has posted a comment (04.05.2012 5:30) 
User B Has posted a comment (04.05.2012 6:30) 
User C has written an article (04.05.2012 7:30) 
user D has posted a comment (04.05.2012 8:30) 

然而,我正與兩個連接這些2和顯示結果掙扎。 閱讀和你有類似的場景香港專業教育學院嘗試這樣的查詢:

$sesid = $_SESSION['user_id']; 
$sql = "SELECT X.* FROM (SELECT * FROM `article` WHERE uniquepostowner = {$sesid} UNION SELECT * FROM `comments` WHERE uniquepostowner = {$sesid}) X ORDER BY X.`date_posted`"; 
$result = mysql_query($sql); 

然後試圖獲取結果是這樣的:

while($row = mysql_fetch_array($result)) 
{ 
echo $row['article.id']; 
} 

表:

**article**: 
id 
title 
content 
uniqueuser 
date_posted 
full_name 
uniquepostowner 

**comments**: 
id 
pid (same as article.id) 
date_posted 
content 
full_name 
uniquepostowner 

有更多的行這些表格,但它們是無關緊要的。 但沒有成功。任何幫助一如既往,非常感謝! :)

目前我正在嘗試這個,但它沒有做我想做的。

$sql = "SELECT * FROM article a INNER JOIN comments c WHERE a.uniquepostowner = {$sesid} AND c.uniquepostowner = {$sesid} 
ORDER BY a.date_posted DESC , c.date_posted DESC "; 
+0

你可以發佈這些表的模式嗎?在這種情況下,我認爲你不需要使用UNION。 – Zagor23 2012-07-13 12:19:19

回答

3

我想這是你所需要的:

更新:

$sesid = $_SESSION['user_id']; 
$sql = " 
SELECT 
    X . * 
FROM 
    (SELECT 
     id, 
     date_posted, 
     content, 
     full_name, 
     uniquepostowner, 
     'article' AS type 
    FROM 
     article 
    WHERE 
     uniquepostowner = {$sesid} UNION SELECT 
     id, 
     date_posted, 
     content, 
     full_name, 
     uniquepostowner, 
     'comment' AS type 
    FROM 
     comments 
    WHERE 
     uniquepostowner = {$sesid}) X 
ORDER BY X.date_posted 
"; 
$result = mysql_query($sql); 

而且可以重複使用:

while($row = mysql_fetch_array($result)) 
{ 
     if($row['type']=='article'){ 
      echo "User " . $row['full_name'] . " has written an article (".$row['date_posted'].")<br>"; 
     } else { 
      echo "User " . $row['full_name'] . " has posted a comment (".$row['date_posted'].")<br>"; 
     } 
} 

你可以檢查出來

http://sqlfiddle.com/#!2/38778/8

+0

我試圖讓它工作,但它返回0行。我需要它獲取文章和評論where uniqueuser = {$ userid}並在date_posted之後排序結果。我認爲這段代碼試圖用相同的id \ pid獲取註釋。這不是必要的。但是,謝謝你的回答,我真的很感激,如果你可以幫助我多一點=) – Havihavi 2012-07-13 13:05:12

+0

我已經更新了答案。我希望這是現在好:) – Zagor23 2012-07-13 13:53:50

+0

感謝噸抽空那裏的人!這正是我想要的!數以百萬計的互聯網給你:) – Havihavi 2012-07-13 17:53:02

2

你應該嘗試修改你的SQL只使用列兩個表的共同點:

SELECT uniquepostowner,date_posted FROM `article` WHERE uniquepostowner = {$sesid} 
UNION 
SELECT uniquepostowner,date_posted FROM `comments` WHERE uniquepostowner = {$sesid} 
ORDER BY date_posted 

有在MySQL manual一些例子。

+0

是的,這就是我現在想要寫的東西... xD – 2012-07-13 12:28:41

+0

這似乎工作,但即時通訊不能單獨迭代結果,所以無法真正看到它是否正常工作,但我希望它,但謝謝你的答案! =) – Havihavi 2012-07-13 13:01:51

0

也許我錯過了點,但不會解決這個問題:

SELECT articles.*, comments.* FROM articles, comments WHERE articles.uniqueuserid = $userId AND comments.postId = articles.Id ORDER BY articles.date_published DESC 

OFC所有的表和字段名稱是需要調整,以適應你的代碼。 它做什麼,基本上是它加載uniquepostowner所有文章或無論你的名字是什麼和相應的評論按發佈日期排序。

這是你在追求什麼?

編輯 其實,評論和文章是特殊的,你想要並排顯示它們嗎?在這種情況下,爲什麼不創建'事件'表,其中包含日期,所有者,類型(文章/評論/喜歡/照片/任何內容)和相應項目的ID,並用它來生成您的Feed?

0

你必須使collumns平等聯合的每個查詢:

SELECT X.* FROM (SELECT **uniquepostowner** FROM `article` WHERE uniquepostowner = {$sesid} UNION SELECT **uniquepostowner** FROM `comments` WHERE uniquepostowner = {$sesid}) X ORDER BY X.`date_posted` 

這只是一個例子,在工會,這兩個查詢必須返回域相同數量的,我sugest你只返回兩張表格都有的文件。