MySQL 1064 Error 语法错误的解决方法是什么?

MySQL 1064 Error what is the way around solving the syntax error?

我需要一些帮助,因为我是 MYSQL 的初学者。我已经尝试查看其他帖子,但没有解决此错误。

您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 7

行 'SELECT max(count) from (Select activity, count(*) from FRIENDS group by activity' 附近使用的正确语法

为什么会产生这个错误,我该如何解决这个问题?

SELECT activity
FROM (SELECT activity, count(*) FROM FRIENDS GROUP BY activity) AS getActivities
WHERE count NOT IN (
    SELECT min(count)
        FROM (SELECT activity, count(*) FROM FRIENDS GROUP BY activity ) AS maximum,
    SELECT max(count)
        FROM (SELECT activity, count(*) FROM FRIENDS GROUP BY activity) AS minimum
)

您的 sql 字符串有误:

  • 不是别名计数 (*) 列

  • 没有第 2 组 table 的条件不在

让我们试试这个查询:

SELECT activity 
FROM ( SELECT activity, COUNT(*) AS Number FROM FRIENDS GROUP BY activity) AS getActivities
WHERE Number NOT IN (SELECT MIN(Number) FROM (SELECT activity, COUNT(*) AS Number FROM FRIENDS GROUP BY activity )  AS maximum)
AND Number NOT IN (SELECT MAX(Number) FROM (SELECT activity, COUNT(*) AS Number FROM FRIENDS GROUP BY activity) AS minimum)

这是您的查询:

select activity
from (Select activity, count(*)
      from FRIENDS
      group by activity
     ) as getActivities
where count not in (Select min(count) from (Select activity, count(*) from FRIENDS group by activity ) as maximum,
                    SELECT max(count) from (Select activity, count(*) from FRIENDS group by activity) as minimum
                   );

您有多个错误。例如:

  • count(*) 没有列别名。但它需要一个名称,因为它在子查询中。
  • 子查询没有 table 别名。
  • 外部 where 中的子查询有括号。
  • , 意味着您打算将子查询作为标量子查询,但它们可以 return 多行。

我想你打算:

select f.activity
from friends f
group by f.activity
having count(*) > (select count(*)
                   from friends f2
                   group by f2.activity
                   order by count(*) asc
                   limit 1
                  ) and
       count(*) < (select count(*)
                   from friends f2
                   group by f2.activity
                   order by count(*) desc
                   limit 1
                  );

如何修复。

select activity from ( Select activity, count(*) as count from FRIENDS group by activity) as getActivities
     where count not in  
 (
   Select min(count) as count from (Select activity, count(*) from FRIENDS group by activity ) as maximum
   union all
   SELECT max(count) as count from (Select activity, count(*) from FRIENDS group by activity) as minimum
 )