mysql子查询从两个表中获取最新评论

时间:2014-04-29 21:03:58

标签: php mysql

我一直致力于查询以获取汇总的通讯列表,例如显示对话的收件箱

下面是我的表格

用户

公司| contact_person | pic_small

警报

comment_id | user_id | poster_id |时间戳

活性

comment_id | user_id |评论|时间戳

评论

comment_id | user_id |评论|时间戳

到目前为止,这是我的工作,虽然我需要一方面的帮助。

SELECT alerts.comment_id,
       alerts.user_id,
       alerts.poster_id,
       alerts.active,
       MAX(alerts.timestamp) AS maxTime,
       users.contact_person,
       users.company,
       users.pic_small
FROM alerts
LEFT JOIN users ON users.user_id = alerts.poster_id
WHERE alerts.user_id = %s
GROUP BY alerts.comment_id
ORDER BY maxTime DESC

评论在活动和评论表中保存,我需要以某种方式加入活动或评论表中的最新(最新)评论(取决于哪个更新)

如何将此添加到我的上述查询中,以下是我想要实现的目标

enter image description here

0 个答案:

没有答案
相关问题