如何找到发布了最多事件数的人

时间:2011-03-31 01:18:37

标签: mysql sql

我正在使用mysql。这是我的sql

  SELECT LIMIT 1
   users.id,
   COUNT(events.id) AS events_count
  FROM
   INNER JOIN 'events' ON users.id = events.user_id
  GROUP BY
    users.id
  ORDER BY
   COUNT(events.id) DESC

这是我得到的错误

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN 'events' ON users.id = events.user_id
      GROUP BY
        users.id' at line 5: 
      SELECT
       users.id,
       COUNT(events.ID) AS events_count
      FROM
       INNER JOIN 'events' ON users.id = events.user_id
      GROUP BY
        users.id
      ORDER BY
       COUNT(events.id) DESC

2 个答案:

答案 0 :(得分:3)

您的FROM子句之后似乎缺少表名。 LIMIT的语法也是错误的。请在此处查看参考http://dev.mysql.com/doc/refman/5.1/en/select.html

SELECT 
  users.id,   
  COUNT(events.id) AS events_count  
FROM users
INNER JOIN events ON users.id = events.user_id  
GROUP BY    
  users.id  
ORDER BY   
  COUNT(events.id) DESC
LIMIT 1   

答案 1 :(得分:0)

LIMIT属于查询的末尾。您是否还以某种方式从查询中删除 users表?

SELECT 
   users.id,
   COUNT(events.id) AS events_count
FROM users
   INNER JOIN events ON users.id = events.user_id
GROUP BY
    users.id
ORDER BY
   events_count DESC
LIMIT 1