开发者

Select * from table with where and order by clause problem

开发者 https://www.devze.com 2022-12-20 22:38 出处:网络
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?

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?

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.

What am I missing exact开发者_开发问答ly?


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)

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

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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号