繁体   English   中英

按日期顺序连接两个表并显示结果

[英]join two tables order by date and display results

我正在尝试使功能类似于Facebook的通知窗口。 它们的功能似乎可以组合任何类型的事件并在日期之后显示它们。

所以我有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)

但是,我都在努力加入这两个并显示结果。 阅读具有类似情况的其他人ive尝试将其作为查询:

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

我认为这是您需要的:

更新:

$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

您应该尝试改写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 手册中还有更多的例子。

也许我错过了要点,但这不能解决问题:

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?

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

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

这只是一个例子,在联合中,两个查询必须返回相同数量的字段,我想您只返回两个表在commom中都具有的filds。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM