dqpu4988 2014-06-13 16:04
浏览 47
已采纳

创建论坛,并尝试按时间戳对两个表进行排序。 一个表主题和其他答复。 我该如何分类呢?

I am trying to sort two tables that I created for a forum by timestamp.

I have searched with no luck yet. Please help.

My first table is "forum_posts" and timestamp is "forum_timestamp" My second table is "forum_posts_replys" is "fpr_timestamp"

With the following query I am able to sort by timestamp or the other, but I would like to sort both. If there are not any replys in a topic yet and it was just posted I would like to see it on the top, but if there is a reply in another topic I would like that reply to show first.

                (SELECT fp.*, fpr.*
            FROM forum_posts fp
            LEFT JOIN forum_posts_replys fpr
            ON fp.forum_post_id = fpr.fpr_post_id
            WHERE `fp`.`forum_id`='$f_id'
            GROUP BY fp.forum_post_id)
            ORDER BY `forum_timestamp` DESC

I also tried GREATEST() and no luck.

The page I am trying to use this on is the main topic listing page. The page just shows topics with their titles and a count of replys and how long ago each was posted.

My current query 06/14/14:

                SELECT sub.*
            FROM
            (SELECT 
             fp.forum_post_id as forum_post_id, fp.forum_id as forum_id, 
             fp.forum_user_id as forum_user_id, fp.forum_title as forum_title, 
             fp.forum_content as forum_content, fp.forum_edit_date as forum_edit_date,
             fp.forum_timestamp as forum_timestamp, fpr.id as id,
             fpr.fpr_post_id as fpr_post_id, fpr.fpr_id as fpr_id,
             fpr.fpr_user_id as fpr_user_id, fpr.fpr_title as fpr_title,
             fpr.fpr_content as fpr_content, fpr.fpr_edit_date as fpr_edit_date,
             fpr.fpr_timestamp as fpr_timestamp,
             ifnull(fpr.fpr_timestamp,forum_timestamp) as tstamp
            FROM forum_posts fp
            LEFT JOIN forum_posts_replys fpr
            ON fp.forum_post_id = fpr.fpr_post_id
            WHERE `fp`.`forum_id`='$f_id'
            GROUP BY fp.forum_post_id) sub

            ORDER BY tstamp DESC

Still only seems to sort by forum_timestamp

  • 写回答

2条回答 默认 最新

  • douchun3680 2014-06-13 16:44
    关注

    Use a subquery.

            SELECT fp.*, fpr.*
            from
    
            (SELECT fp.*, fpr.*, ifnull(fpr.fpr_timestamp,forum_timestamp) as tstamp
            FROM forum_posts fp
            LEFT JOIN forum_posts_replys fpr
            ON fp.forum_post_id = fpr.fpr_post_id
            WHERE `fp`.`forum_id`='$f_id'
            GROUP BY fp.forum_post_id) sub
    
            order by tstamp desc
    

    Edit: List the wanted fields explicitly and use the table alias for the subquery:

            SELECT sub.*
            from
    
            (SELECT fp.field_1 as f1, fp.field_2 as f2, fpr.field_1 as f3, fpr.field_2 as f4, ifnull(fpr.fpr_timestamp,forum_timestamp) as tstamp
            FROM forum_posts fp
            LEFT JOIN forum_posts_replys fpr
            ON fp.forum_post_id = fpr.fpr_post_id
            WHERE `fp`.`forum_id`='$f_id'
            GROUP BY fp.forum_post_id) sub
    
            order by tstamp desc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程