在SQL中创建视图并接收错误

时间:2013-11-17 22:25:36

标签: sql oracle view

我知道这是一个简单的修复,但我似乎无法弄清楚如何更改代码,所以我没有收到此错误:

ORA-00918:列模糊定义

以下是代码:

CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS    
SELECT BRANCH_NUM, ON_HAND 
FROM BRANCH, INVENTORY
WHERE BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM
ORDER BY BRANCH.BRANCH_NUM;

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

我猜你需要指定你想要的BRANCH_NUM表:

CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS    
SELECT BRANCH.BRANCH_NUM, ON_HAND 
FROM BRANCH, INVENTORY
WHERE BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM
ORDER BY BRANCH.BRANCH_NUM;

此外,您可能需要考虑使用后ANSI SQL-92语法进行连接:

CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS    
SELECT BRANCH.BRANCH_NUM, ON_HAND 
FROM BRANCH
INNER JOIN INVENTORY ON BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM
ORDER BY BRANCH.BRANCH_NUM;

有关它的一些推理,请参阅Bad habits to kick : using old-style JOINs

答案 1 :(得分:1)

您正在制作SELECT BRANCH_NUM, ON_HAND而未确定哪些列来自哪些列。可能是其中一个存在于两个表中,例如将其修改为

SELECT INVENTORY.BRANCH_NUM, INVENTORY.ON_HAND 
-- ...

来自INVENTORY表内的那些列。)

答案 2 :(得分:0)

CREATE VIEW BOOK_INVENTORY (BRANCH_NUM, UNITS) AS    
SELECT BRANCH.BRANCH_NUM, INVENTORY.ON_HAND 
FROM BRANCH, INVENTORY
WHERE BRANCH.BRANCH_NUM = INVENTORY.BRANCH_NUM
GROUP BY BRANCH.BRANCH_NUM, INVENTORY.ON_HAND 
ORDER BY BRANCH.BRANCH_NUM;

PS这个查询没有多大意义 - 为什么要分组?你也加入了两张表,没有明显的理由。这样做的唯一方法是排除无效的BRANCH