【发布时间】:2013-10-03 08:21:42
【问题描述】:
我想知道使用WHERE子句和使用内连接ON中的匹配有什么区别。
这种情况下的结果是一样的。
第一个查询:
with Catmin as
(
select categoryid, MIN(unitprice) as mn
from production.Products
group by categoryid
)
select p.productname, mn
from Catmin
inner join Production.Products p
on p.categoryid = Catmin.categoryid
and p.unitprice = Catmin.mn;
第二次查询:
with Catmin as
(
select categoryid, MIN(unitprice) as mn
from production.Products
group by categoryid
)
select p.productname, mn
from Catmin
inner join Production.Products p
on p.categoryid = Catmin.categoryid
where p.unitprice = Catmin.mn; // this is changed
结果两个查询:
【问题讨论】:
-
在这种情况下,两者的结果与您从 2 个表进行比较的结果相同。第一个将只选择符合条件的那些进行绑定,第二个将绑定两个表并过滤掉条件。
-
在 SSMS 工具栏上寻找一个按钮,工具提示显示“包括实际执行计划” - 单击它,然后运行两个查询并进行比较。
标签: sql tsql sql-server-2012