doupian9490 2012-07-13 12:14
浏览 92
已采纳

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

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条回答 默认 最新

  • doom910730 2012-07-13 12:51
    关注

    I think this is what you need:

    UPDATED:

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

    And you can iterate with:

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

    You can check it out at:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示