我有下面的数据库表
我想向带有 owner_id
的用户显示通知 1. 通知显示告诉所有者有两个用户(ID 为 17 和 2)用 post_id< 评论了他/她的帖子
1。
我尝试过以下查询,但它返回 2 行。我如何构建查询以返回一行,因为我希望一起返回一篇文章的通知?谢谢。
SELECT DISTINCT commenters_id, id, owner_id,
post_id, type, UNIX_TIMESTAMP(date_done) AS date
FROM notification
GROUP BY commenters_id
HAVING owner_id = '$user_id' AND commenters_id != '$user_id'
ORDER BY date_done DESC
最佳答案
此代码不会为您提供commenters_id,而是会统计每个帖子的回复人数。 日期
将是某人最后一次回复该特定帖子的时间:
SELECT
COUNT(DISTINCT commenters_id) AS commenter_count,
owner_id,
post_id,
type,
UNIX_TIMESTAMP(MAX(date_done)) AS date
FROM notification
WHERE owner_id = '$user_id' AND commenters_id != '$user_id'
GROUP BY owner_id, post_id, type
ORDER BY UNIX_TIMESTAMP(MAX(date_done)) DESC
关于php - 我如何构造这种sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10186974/