我试图获得更高的价格最贵的产品(笔记本电脑,PC或打印机)的。所以我试过这个查询:SQL Server查询不正确过滤
select price
from
(select price, model
from printer
union
select price, model
from pc
union
select price, model
from laptop) t
where
price >= All (select t.price);
但是这会返回所有价格。如果我改变最后一行是:
where price > All (select t.price);
我没有得到任何结果。
那么为什么呢?我试着最后一行是:where price >= All (select price from t);
,但它不起作用(它说t是无效的对象 - 为什么?)。
有人能告诉我如何解决这个查询?
我接受这样做的更好的方法的建议,但我会很感激,如果有人能解决这个问题的查询,并使得它的工作原理。
感谢您的关注
附:我认为(select t.price);
不产生价格的整个列表,但我怎么能生成它在该子查询时(select price from t)
无效的请求?
要回答你的第一个问题:'选择t.price'是_correlated subquery_。你已经有效地写了'where t.price> = t.price'对于所有非空价格都是'true'。将条款更改为'where t.price> t.price'结果为零行,因为没有价格大于自身。 – HABO