无效的列名?

时间:2018-07-12 10:38:03

标签: sql error-handling

出什么问题了?

SELECT distinct a.number,a.code,a.day - (b.Miktar - b.GonderilenMiktar) AS sonuc FROM dbo.T07 a, T05 b WHERE a.number=b.number and a.code=b.code and sonuc!=-1

错误:

无效的列名“ sonuc”。

2 个答案:

答案 0 :(得分:1)

您可以修改

 SELECT distinct a.number,a.code,a.day - (b.Miktar - b.GonderilenMiktar) AS sonuc
 from dbo.T07 a join
       T05 b 
       on a.number = b.number and a.code = b.code
 where (b.Miktar - b.GonderilenMiktar)!= -1

答案 1 :(得分:0)

列别名不能在相同的selectwhere或其他子句中使用。您需要CTE或子查询:

select distinct number, code, sonuc
from (select a.number, a.code,
             a.day - (b.Miktar - b.GonderilenMiktar) as sonuc
      from dbo.T07 a join
           T05 b 
           on a.number = b.number and a.code = b.code
     ) ab
where sonuc <> -1;

注意:

  • 从不FROM子句中使用逗号。 始终使用正确的,明确的,标准 JOIN语法。
  • 不等于的标准SQL运算符是<>