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
精彩评论