简体   繁体   中英

Select * from table with where and order by clause problem

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 exactly?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM