我正在尝试从两个表中查询数据,并不断出现“列定义不明确”错误。
我的查询如下:
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;
在选择语句中出错。
答案 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
. . .