我不明白为什么这个查询不能工作:
SELECT product.maker, product.model, product.type FROM product EXCEPT
(Select top 3 with ties product.maker, product.model, product.type
FROM Product ORDER BY model DESC)Product(maker, model, type)
PC(code, model, speed, ram, hd, cd, price)
Laptop(code, model, speed, ram, hd, screen, price)
Printer(code, model, color, type, price)更新:我实际上尝试过使用mssql在im的在线站点输入这段代码,但仍然不起作用。这会不会是一个可能的站点错误(即他们的DBMS有问题)。* NOT EXIST似乎也不起作用,但我以前在网站上的其他查询问题中使用过它。
发布于 2014-06-06 12:19:40
Yoy也可以尝试此查询
SELECT product.maker, product.model, product.type FROM product
MINUS
Select top 3 with ties product.maker, product.model, product.type
FROM Product ORDER BY model DESC.希望借此你能达到预期的效果。
发布于 2014-06-06 16:27:35
EXCEPT和TOP是mssql (t-sql)关键字。您是否使用MSSQL或MySQL,如您的问题的标签中所述。如果您尝试在MySQL上运行此查询,这是错误的原因,因为MySQL不支持这些关键字。
在MySQL中,尝试以下等效项:
SELECT DISTINCT
product.maker,
product.model,
product.type FROM product
LEFT JOIN
(SELECT DISTINCT product.maker, product.model, product.type
FROM Product
ORDER BY model DESC
LIMIT 3
) as p2
ON product.maker=p2.maker
AND product.model = p2.model
AND product.type = p2.type
WHERE p2.maker IS NULL
AND p2.model IS NULL
AND p2.type IS NULL注意:为了模拟EXCEPT,我在主查询中添加了DISTINCT
EXCEPT返回左侧查询中未在右侧查询中找到的任何非重复值
为了模拟TOP WITH TIES,还在子查询中添加了DISTINCT
https://stackoverflow.com/questions/24074010
复制相似问题