1

我有一个查询,它有几个关节非常复杂,我让它返回用户的所有消息,以及该消息的最新帖子(即消息帖子上的 max(id) 部分)。但我也想显示特定消息的帖子总数。我不想提取所有消息,而只是获取行的 count() 。

这是我的查询,现在可以使用,没有计数部分:

SELECT recipients.readmessage, messages.id AS id, messages.subject, messages.createdat AS messagecreated, messages.userid AS originalposterid, messages.usershortname AS opname, messageposts.id AS messagepostid, messageposts.usershortname AS repliername, messageposts.userid AS replierid, messageposts.updatedat AS messagepostcreated, userpictures.id AS pictureid, userpictures.createdat AS picturedate, userpictures.name AS picname, users.sex 
    FROM recipients 
        INNER JOIN messages ON recipients.messageid = messages.id 
        AND messages.deletedat IS NULL 
            LEFT OUTER JOIN messageposts
            ON messageposts.id =
                 (
                 // HERE IS WHERE I WANT A COUNT//
                 SELECT  max(id) 
                 FROM    messageposts
                 WHERE   messageid = messages.id
                 AND messageposts.deletedat IS NULL
                 )
                INNER JOIN users ON messageposts.userid = users.id 
                AND users.deletedat IS NULL 
                    LEFT OUTER JOIN userpictures 
                    ON userpictures.id =
                             (
                             SELECT  max(id)
                             FROM    userpictures
                             WHERE   userid = users.id
                             AND userpictures.deletedat IS NULL
                             )

    WHERE ( recipients.userid = #currentuser.id# ) 
    AND ( recipients.deletedat IS NULL ) 
    ORDER BY recipients.readmessage ASC,messageposts.updatedat DESC,messages.createdat DESC,userpictures.createdat DESC

我需要插入的部分是这样的:

(
SELECT COUNT(id) as totalposts
FROM    messageposts
WHERE   messageid = messages.id
)

这是我尝试过的:

(
 SELECT  max(id) 
 FROM       
    (
    SELECT COUNT(id) as totalposts
    FROM    messageposts
    WHERE   messageid = messages.id
    )
    messageposts
 WHERE      messageid = messages.id
 AND messageposts.deletedat IS NULL
 )

但这给了我一个模棱两可的列名错误。我尝试将其更改为:

(
 SELECT  max(id) 
 FROM       
    (
    SELECT COUNT(id) as totalposts
    FROM    messageposts
    WHERE   messageposts.messageid = messages.id
    )
    messageposts
 WHERE   messageposts.messageid = messages.id
 AND messageposts.deletedat IS NULL
 )

但后来我得到无效的列名messageid。

关于如何让它发挥作用的任何想法?

4

1 回答 1

0

我认为您需要更改链接到messageposts子查询的方式:

老的:

LEFT OUTER JOIN messageposts
    ON messageposts.id =
             (
             // HERE IS WHERE I WANT A COUNT//
             SELECT  max(id) 
             FROM    messageposts
             WHERE   messageid = messages.id
             AND messageposts.deletedat IS NULL
             )

新的:

LEFT OUTER JOIN
    (
    SELECT 
        messageid AS postmessageid
        ,max(id) AS LatestPost
        ,count(id) AS TotalPosts
    FROM messageposts
    WHERE deletedat IS NULL
    GROUP BY messageid
    ) PostsOnMessage
ON messages.id = PostsOnMessage.postmessageid

然后,您可以在上面的字段中添加LatestPost和。TotalPosts

于 2013-03-14T12:09:53.417 回答