如何使用mysql从特定行的多个表中获取最新日期

时间:2015-07-21 09:18:13

标签: php mysql phalcon

enter image description here我正在尝试开发像facebook这样的功能,现在在主页上我希望通过最新活动获得所有帖子和状态排序,例如,评论,分享。下面尝试了一些提问:

 (
            SELECT '' as commentstatusid,'' as commentpostid,'' as sharestatusid,'' as sharepostid, '' as likestatusid,'' As lid, s.id AS sid, '' AS shid,'' AS cid  , s.createdDate AS d
            FROM  `tblstatusmanagement` as s LEFT JOIN tblcommentsmanagement as c on s.id = c.statusid WHERE s.status = 'Active' AND s.auther_id IN (1) 
            )
            UNION (
            SELECT  '' as commentstatusid,'' as commentpostid,sh.statusid as sharestatusid,postid as sharepostid,'' as likestatusid,'',  '', sh.id AS shid,'', MAX(createdDate) AS d
            FROM  `tblsharemanagement` as sh LEFT JOIN tblstatusmanagement as s on sh.statusid = s.id 
            LEFT JOIN `tblpostmanagement` as p on p.id = sh.postid 
            WHERE status = 'Active' AND sh.shareuser IN (1) 
            )
    UNION (

            SELECT  '' as commentstatusid,'' as commentpostid,'' as sharestatusid,'' as sharepostid,statusid as likestatusid,id As lid , '', '','', MAX(createdDate) AS d
            FROM  `tbllikesmanagement` as l LEFT JOIN `tblsharemanagement` as sh on sh.id = l.shareid or s.id = l.statusid WHERE status = 'Active' AND user IN (1) 
            )
    UNION (

            SELECT   statusid as commentstatusid,postid as commentpostid,'' as sharestatusid, '' as sharepostid,'' as likestatusid,'','', '', id As cid, MAX(createdDate) AS d
            FROM  `tblcommentsmanagement` as c WHERE s.id =  c.statusid status = 'Active' AND userid IN (1) group by postid,statusid
            )
            ORDER BY d DESC
SELECT s.id AS sid, sh.id AS shid, max( s.createdDate ) AS statusdate, max( sh.createdDate ) AS sharedate, max( l.createdDate ) AS likedate, max( c.createdDate ) AS commentdate
FROM tblstatusmanagement s
LEFT JOIN tblsharemanagement sh ON s.id = sh.statusid
LEFT JOIN tbllikesmanagement l ON l.statusid = s.id
OR l.shareid = sh.id
LEFT JOIN tblcommentsmanagement c ON c.statusid = s.id
GROUP BY s.id, sh.id
ORDER BY max( s.createdDate ) DESC , max( sh.createdDate ) DESC , max( l.createdDate ) DESC , max( c.createdDate ) DESC
LIMIT 0 , 30

0 个答案:

没有答案