日期:2014-05-18  浏览次数:20403 次

sql2005查询语句问题
查询所有的类别信息 当num为0时查询所有的,num为1时则根据id查询一条
if (num == 0) {
sql = "SELECT l.id,l.name,l.shuoming,count(p.lid),l.contenttime FROM photo AS p "
+ " RIGHT JOIN leibie AS l ON p.lid=l.id GROUP BY l.id ";
} else if (num == 1) {
sql = "SELECT l.id,l.name,l.shuoming,count(p.lid),l.contenttime FROM photo AS p "
+ " RIGHT JOIN leibie AS l ON p.lid=l.id where l.id="
+ id
+ " GROUP BY l.id ";
提示错误:com.microsoft.sqlserver.jdbc.SQLServerException: 选择列表中的列 'leibie.name' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。向各位高手请教啊

------解决方案--------------------
SQL code
if (num == 0) {
sql = "SELECT l.id,l.name,l.shuoming,count(p.lid),l.contenttime FROM photo AS p "
+ " RIGHT JOIN leibie AS l ON p.lid=l.id GROUP BY l.id,l.name,l.shuoming ,l.contenttime";
} else if (num == 1) {
sql = "SELECT l.id,l.name,l.shuoming,count(p.lid),l.contenttime FROM photo AS p "
+ " RIGHT JOIN leibie AS l ON p.lid=l.id where l.id="
+ id
+ " GROUP BY l.id,l.name,l.shuoming,l.contenttime ";


---GROUP BY l.id,l.name,l.shuoming,l.contenttime