1

我有一个查询,除了从与帖子相关的评论中获取最大日期外,它运行良好。

所以我想做的是:

  • 关系: 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
4

1 回答 1

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
于 2012-12-20T10:41:24.780 回答