-1

我收到此错误 - 在第 5 行的“int inner join categories as cat on int.categoryID = cat.categoryID GRO”附近使用正确的语法

我的 sql -

SELECT interest_desc, categoryID, category_desc, MAX(num_in_cat) AS num_in_cat 
    FROM
    (
       SELECT int.interest_desc, int.category_ID, cat.category_desc, 
              COUNT(categoryID) AS num_in_cat
       FROM 
          interests as int 
          inner join categories as cat 
             on int.categoryID = cat.categoryID

       GROUP BY int.interest_desc, int.categoryID
     ) subsel 
     GROUP BY interest_desc, categoryID"

谢谢

4

3 回答 3

1

您需要反引号您int的 s 因为它们是保留关键字。

或者,使用不同的别名。

SELECT interest_desc, categoryID, category_desc, MAX(num_in_cat) AS num_in_cat  
FROM 
( 
   SELECT `int`.interest_desc, `int`.category_ID, cat.category_desc,  
          COUNT(categoryID) AS num_in_cat 
   FROM  
      interests as `int`  
      inner join categories as cat  
         on `int`.categoryID = cat.categoryID 

   GROUP BY `int`.interest_desc, `int`.categoryID 
 ) subsel  
 GROUP BY interest_desc, categoryID" 
于 2012-05-03T07:09:07.783 回答
1

interests为您的表使用不同的别名。

另外 - 您需要整理以下内容:

  • categoryIDvs category_ID- 哪一个是正确的?
  • count(categoryID)是模棱两可的,因为它categoryID同时存在于categoriesinterests表中

假设该列categoryID在两个表上,以下应该有效:

SELECT interest_desc, categoryID, category_desc, MAX(num_in_cat) AS num_in_cat 
    FROM
    (
       SELECT intr.interest_desc, intr.categoryID, cat.category_desc, COUNT(cat.categoryID) AS num_in_cat
       FROM interests as intr inner join categories as cat on 
intr.categoryID = cat.categoryID
       GROUP BY intr.interest_desc, intr.categoryID
     ) subsel 
     GROUP BY interest_desc, categoryID;
于 2012-05-03T07:14:17.760 回答
0

命名表int是一个问题。它可以通过引用表名来解决,但你真的应该重命名它。为了你的理智,以及那些将在你之后努力的人。

于 2012-05-03T07:06:37.663 回答