简体   繁体   English

从带有位置和子句问题的表中选择*

[英]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? 我需要获取有评论的项目数量,但我无法获得此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. 列“ TableName.ID”在ORDER BY子句中无效,因为它既不在聚合函数中也不在GROUP BY子句中。

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. 您正在选择一个计数,因此Order By子句毫无意义。 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. 只需删除“ Order By”子句。 It's unnecessary. 没必要

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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