db的最新结果

时间:2011-05-20 18:42:20

标签: mysql sql

我只需要为最新项目显示两个连接表的结果。我有以下查询:

  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

出于某种原因,我继续获得第一个项目......我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

你肯定是在正确的轨道上。您的汇总GROUP BY似乎缺少COUNT()条款。试试这个:

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
GROUP BY project.id, project.created
ORDER BY project.created DESC,
LIMIT 1