嗨,有人可以帮我解决这个问题。我花了很多年时间试图弄清楚出了什么问题,但没有运气
表格
问题
编写一个SQL命令,以显示每个拥有多个作者的发布者,发布者的名称,发布者的位置以及发布商销售的图书的平均成本。
代码:
SELECT Book.publishername, location, avg(cost)
FROM Publisher
, Book
WHERE Publisher.publisherName = Book.publisherName
GROUP
BY publisherName
HAVING COUNT (DISTINCT authorname) >1
错误
ORA-00918:列模糊定义
正确答案:
SELECT Publisher.publisherName, Publisher.location, avg(cost)
FROM Publisher, Book
WHERE Publisher.publisherName = Book.publisherName
GROUP BY Publisher.PublisherName, Publisher.Location
HAVING COUNT (DISTINCT authorname) >1;
答案 0 :(得分:1)
您的选择中有不明确的列名称。 select语句中的location
可以来自Publisher表或Author表,GROUP BY中的publisherName
可以来自Publisher或Book。您需要明确指定您希望这些值来自哪些表。
SELECT Book.publishername, Publisher.location, avg(cost)
FROM Publisher, Book
WHERE Publisher.publisherName =Book.publisherName
GROUP BY publisher.publisherName
HAVING COUNT (DISTINCT authorname) >1;
编辑:关于您的评论,您的选择中有一列不在Group By中,而不是聚合函数(例如SUM,AVG等)。您需要在GROUP BY中包含location
:
SELECT Book.publishername, Publisher.location, avg(cost)
FROM Publisher, Book
WHERE Publisher.publisherName =Book.publisherName
GROUP BY publisher.publisherName, Publisher.location
HAVING COUNT (DISTINCT authorname) >1;
答案 1 :(得分:0)
不应该是GROUP BY Book.publisherName吗?
答案 2 :(得分:0)
位置在两个表中。您必须使用Table.Column:
来定义它Publisher.location
答案 3 :(得分:0)
你的小组应该阅读 GROUP BY Book.publishername
答案 4 :(得分:0)
答案确实解释了发生了什么。这是您的查询:
SELECT Book.publishername, location, avg(cost)
FROM Publisher, Book
WHERE Publisher.publisherName =Book.publisherName
GROUP BY publisherName
---------^
HAVING COUNT (DISTINCT authorname) >1;
Oracle不知道列的来源。它可以来自任何一个表。因此,使用表别名为所有列添加前缀。此外,使用正确的连接语法,而不是隐式连接。这是您查询的更好版本:
SELECT b.publishername, p.location, avg(b.cost)
FROM Publisher p join
Book b
on p.publisherName = b.publisherName
GROUP BY b.publisherName
HAVING COUNT(DISTINCT b.authorname) > 1;
我猜这些列来自哪些表。