SQL错误-“列定义不明确”

时间:2018-06-24 15:01:17

标签: sql oracle

我正在尝试从两个表中查询数据,并不断出现“列定义不明确”错误。

我的查询如下:

SELECT deptno AS "Dept No", dname AS "Dept Name", sum(sal) AS "Sal Cost" 
FROM emp, dept 
WHERE emp.deptno = dept.deptno 
GROUP BY dname;

在选择语句中出错。

1 个答案:

答案 0 :(得分:1)

每当查询中有多个表引用时,都应始终限定 all 列名。然后,您将永远不会再收到此错误:

SELECT d.deptno AS "Dept No", d.dname AS "Dept Name", sum(e.sal) AS "Sal Cost" 
FROM emp e JOIN
     dept d
     ON e.deptno = d.deptno 
GROUP BY d.deptno, d.dname;

请注意,您必须按SELECT中所有所有未聚合的列进行聚合。

我也强烈建议您使用不需要转义的列名:

SELECT d.deptno, d.dname, sum(e.sal) AS salary_cost
. . .