没有为< tbl'的第1列指定列名。

时间:2015-06-23 10:56:37

标签: sql-server

在sql server 208 r2中执行以下查询时 我收到错误消息没有为< tbl'的第1列指定列名。

SELECT rollno, 
       classid, 
       t_class.classname 
FROM   t_class 
       LEFT JOIN(SELECT Count(classname), 
                        classname 
                 FROM   t_class 
                 GROUP  BY groupname 
                 HAVING Count(classname) > 1)tbl 
              ON tbl.classname = t_class.classname 

1 个答案:

答案 0 :(得分:2)

由于您在JOIN中使用子查询,因此您的列需要具有名称:

SELECT rollno, 
       classid, 
       t_class.classname 
FROM   t_class 
       LEFT JOIN(SELECT Count(classname) as CountOfClasses, 
                        classname 
                 FROM   t_class 
                 GROUP  BY groupname 
                 HAVING Count(classname) > 1)tbl 
              ON tbl.classname = t_class.classname

但是,由于您未在查询中使用此聚合字段,因此您实际上可以将其删除:

SELECT rollno, 
       classid, 
       t_class.classname 
FROM   t_class 
       LEFT JOIN(SELECT classname 
                 FROM   t_class 
                 GROUP  BY groupname 
                 HAVING Count(classname) > 1)tbl 
              ON tbl.classname = t_class.classname