MySQL一对多从子日期选择max UNIX_TIMESTAMP()

时间:2012-12-20 10:33:09

标签: mysql sql

除了从与帖子相关的评论中获取最大日期之外,我的查询工作正常。

所以我想做的是:

  • 关系:1个帖子到很多评论
  • 使用max()获取UNIX_TIMESTAMP()中每个帖子的“最新评论日期”
  • 按“最新发布日期”或“最新评论日期”对整个查询进行排序。因此按每行排除的“最新发布日期”或“最新评论日期”排序。
  • 因此,顶部的记录是最新的帖子,或者如果帖子的评论稍后会发布到顶部的任何帖子。

非常感谢任何帮助。

查询无法获取最新的最新评论日期并按最新的

排序
SELECT DISTINCT wallposts.p_id,wallposts.type,wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,wallposts.tagedpersons,wallposts.title AS thetitle,wallposts.url,wallposts.description,wallposts.cur_image,wallposts.uip,wallposts.likes,wallposts.userid,wallposts.posted_by,wallposts.post as postdata,wallusers.*, UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,wallposts.date_created,wallposts.course 
        FROM wallposts,wallusers
        where (
        wallposts.userid =4276 OR
        wallposts.tagedpersons LIKE '%4276%' OR
        EXISTS (SELECT * FROM wallcomments WHERE wallposts.p_id = wallcomments.post_id AND wallcomments.tagedpersons LIKE '%4276%')
        ) 
        AND wallusers.mem_id = wallposts.userid
        order by wallposts.p_id desc 

我试图解决问题的那个但是失败了:

   SELECT DISTINCT wallposts.p_id,wallposts.type,wallposts.value,wallposts.media,wallposts.youtube,wallposts.post_type,wallposts.tagedpersons,wallposts.title AS thetitle,wallposts.url,wallposts.description,wallposts.cur_image,wallposts.uip,wallposts.likes,wallposts.userid,wallposts.posted_by,wallposts.post as postdata,wallusers.*, UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,wallposts.date_created,wallposts.course 
                FROM wallposts,wallusers
                JOIN wallusers wu on wallposts.userid = wu.mem_id
                LEFT JOIN wallcomments wc ON wc.post_id(SELECT date_created as commentdate_created, UNIX_TIMESTAMP() - max(date_created) as latestcomment
                FROM wallcomments wc WHERE wallposts.p_id = wc.post_id LIMIT 1)
                where (
                wallposts.userid = 4276 OR
                wallposts.tagedpersons LIKE '%4276%' OR
                EXISTS (SELECT * FROM wallcomments WHERE wallposts.p_id = wallcomments.post_id AND wallcomments.tagedpersons LIKE '%4276%')
                ) 
                order by greatest(latestcomment, TimeSpent) DESC

1 个答案:

答案 0 :(得分:0)

查询本身对我来说有点奇怪 - LEFT JOIN似乎已损坏,请再次检查:

SELECT 
      DISTINCT wallposts.p_id,
      wallposts.type,
      wallposts.value,
      wallposts.media,
      wallposts.youtube,
      wallposts.post_type,
      wallposts.tagedpersons,
      wallposts.title AS thetitle,
      wallposts.url,
      wallposts.description,
      wallposts.cur_image,
      wallposts.uip,
      wallposts.likes,
      wallposts.userid,
      wallposts.posted_by,
      wallposts.post as postdata,
      wallusers.*,
      UNIX_TIMESTAMP() - wallposts.date_created AS TimeSpent,
      wallposts.date_created,
      wallposts.course 
FROM wallposts, wallusers
      JOIN wallusers wu 
            on wallposts.userid = wu.mem_id
      LEFT JOIN wallcomments wc 
            ON wc.post_id (
                  SELECT 
                        date_created as commentdate_created, 
                        UNIX_TIMESTAMP() - max(date_created) as latestcomment
                  FROM wallcomments wc 
                  WHERE wallposts.p_id = wc.post_id LIMIT 1
            )
WHERE (
            wallposts.userid = 4276 OR
            wallposts.tagedpersons LIKE '%4276%' OR
            EXISTS (
                  SELECT * 
                  FROM wallcomments 
                  WHERE wallposts.p_id = wallcomments.post_id 
                        AND wallcomments.tagedpersons LIKE '%4276%'
            )
      )
order by greatest(latestcomment, TimeSpent) DESC