【问题标题】:MySQL one to many subselect max UNIX_TIMESTAMP() from dateMySQL 一对多子选择 max UNIX_TIMESTAMP() from date
【发布时间】:2012-12-20 10:33:09
【问题描述】:

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

所以我想做的是:

  • 关系:1 个帖子到许多 cmets
  • 使用 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

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    查询本身对我来说有点奇怪 - 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
    

    【讨论】:

    • 什么意思 LEFT JOIN 似乎已损坏。我不确定我是否需要使用 LEFT JOIN。我将您的代码复制并粘贴到 phpmyadmin 中,这是错误 - #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 27 行的“SELECT date_created as commentdate_created,”附近使用正确的语法
    • 我所做的一切 - 格式化给定的代码,使其可读
    • 好的,并没有真正帮助我在写的时候已经读过了。为什么不直接编辑我的问题并重新格式化。
    猜你喜欢
    • 2017-11-24
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多