1

I'm trying to make a function kinda similar to facebook's notification window. Their function seems to combine any kind of event and display them after date.

So i have 2 tables : Article and comments. both have 2 similar rows : uniquepostowner and date_posted.

I want to join them up and display them one after another. Example :

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)

However, i'm struggling with both joining these 2 and displaying the results. Reading others with similar scenarios ive tried this as a query:

$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);

Then trying to fetch the results like this:

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

Tables:

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

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

there are more rows to these tables, but they are irrelevant. But no success. Any help is greatly appreciated as always! :)

Currently i'm trying this, but its not doing what i want.

$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 ";
4

4 回答 4

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

于 2012-07-13T12:51:10.457 回答
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手册中有更多示例。

于 2012-07-13T12:24:46.657 回答
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,并使用它来生成您的提要?

于 2012-07-13T12:25:09.040 回答
0

您必须使联合的每个查询的列相等:

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

这只是一个示例,在联合中,两个查询必须返回相同数量的字段,我建议您只返回两个表共有的字段。

于 2012-07-13T12:28:19.633 回答