数据库的最新结果
我需要仅显示最新项目的两个连接表的结果。我有以下疑问:
SELECT project.id,
project.created,
COUNT(DISTINCT events.user_id) AS cnt
FROM project
JOIN events ON (events.project_id = project.id)
WHERE project.creator = $creatorID
AND events.user_id != $creatorID
ORDER BY project.created DESC
LIMIT 1
出于某种原因,我继续获得第一个项目...我在这里缺少什么?
I need display results from two joined tables only for the latest project. I have the following query:
SELECT project.id,
project.created,
COUNT(DISTINCT events.user_id) AS cnt
FROM project
JOIN events ON (events.project_id = project.id)
WHERE project.creator = $creatorID
AND events.user_id != $creatorID
ORDER BY project.created DESC
LIMIT 1
For some reason I keep on getting the first project... What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绝对走在正确的道路上。您似乎缺少聚合
COUNT()
的GROUP BY
子句。试试这个:You're definitely on the right track. It seems you're missing a
GROUP BY
clause for your aggregateCOUNT()
. Try this: