从有where和order by子句问题的表中选择*

时间:2021-08-18 22:43:49

I need to get the number of items that have a comment but I cant get this SQL statement to work for me........any advice?

我需要得到有评论的项目的数量,但是我不能让这个SQL语句为我工作…任何建议吗?

Select count(Name) as TotalComments 
from TableName where comment <> '' 
order by ID 

Error Message:

错误信息:

Column "TableName.ID" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.

表列”。“ID”在ORDER BY子句中无效,因为它不包含在聚合函数或GROUP BY子句中。

What am I missing exactly?

我到底错过了什么?

2 个解决方案

#1


4  

Wait a minute...

等一下…

Select count(Name) as TotalComments  
from TableName where comment <> ''  
order by ID 

You're selecting a count, so the Order By clause is pointless. You should be getting a scalar result. ( a single value, not a set if rows)

您正在选择一个计数,所以Order By子句是没有意义的。你应该得到一个标量的结果。(单个值,而不是集合if行)

Is this a trick question? It's too early for that.

这是一个难题吗?现在还为时过早。

Simply remove the "Order By" clause. It's unnecessary.

只需删除“Order By”子句。这是不必要的。

#2


0  

try this (I tried it in Sql server not in MySql)

尝试一下(我在Sql server中尝试过,而不是在MySql中)

SELECT     Name, COUNT(ID) AS TotalComments
FROM       TableName
WHERE     (Comment IS NOT NULL)
GROUP BY Name
ORDER BY TotalComments

#1


4  

Wait a minute...

等一下…

Select count(Name) as TotalComments  
from TableName where comment <> ''  
order by ID 

You're selecting a count, so the Order By clause is pointless. You should be getting a scalar result. ( a single value, not a set if rows)

您正在选择一个计数,所以Order By子句是没有意义的。你应该得到一个标量的结果。(单个值,而不是集合if行)

Is this a trick question? It's too early for that.

这是一个难题吗?现在还为时过早。

Simply remove the "Order By" clause. It's unnecessary.

只需删除“Order By”子句。这是不必要的。

#2


0  

try this (I tried it in Sql server not in MySql)

尝试一下(我在Sql server中尝试过,而不是在MySql中)

SELECT     Name, COUNT(ID) AS TotalComments
FROM       TableName
WHERE     (Comment IS NOT NULL)
GROUP BY Name
ORDER BY TotalComments