两个子查询单独工作正常,但是 "SQL command not properly ended" 当通过 except 加入时 [像我五岁一样解释]

Two subqueries work fine individually but "SQL command not properly ended" when join by except [Explain like I'm five]

我看了很多类似的帖子,但我仍然找不到类似的案例,或者我太初学者了,无法首先理解问题中的命令。

我写的命令 returning ORA-00933: SQL command not properly ended:

(select product.productid, productname, productprice 
from product, soldvia 
where product.productid = soldvia.productid 
group by product.productid, product.productname, product.productprice 
having sum(soldvia.noofitems) > 3 )

except 

(select product.productid, productname, productprice 
from product, soldvia 
where product.productid = soldvia.productid 
group by product.productid, product.productname, product.productprice 
having count(soldvia.tid) > 1);

当我 运行 个人 select 命令时,他们 运行 很好并且 return 了预期的结果。

编辑:我正在学习 EXCEPT 使用此命令,因此,我必须使用该命令来完成任务。 任务是:

Retrieve the product ID, product name, and product price for each product that has more than three items sold within all sales transactions but whose items were not sold in more than one sales transaction

except 在 Oracle 中不是一个东西;等效关键字是 minus:如果您更改关键字,您的查询应该就可以正常工作。

另一方面,两个查询完全相同,因此您可以合并 having 子句:

select p.productid, p.productname, p.productprice 
from product p
inner join soldvia s on p.productid = s.productid 
group by p.productid, p.productname, p.productprice 
having sum(s.noofitems) > 3 and count(s.tid) <= 1

备注:

  • 总是 使用标准的显式连接(使用 on 关键字)而不是老式的隐式连接(在from 子句):不应在新代码中使用此旧语法

  • table 别名使查询更易于编写和阅读

  • 在多table查询中,总是用它们所属的table限定所有列名,所以查询明确且更容易理解