我想知道在使用WHERE子句或使用內連接的ON中的匹配方面是否有任何區別。與ON子句相比,WHERE子句中的過濾有什麼區別?
在這種情況下的結果是相同的。
首先查詢:
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工具欄上尋找一個按鈕,提示「包含實際執行計劃」 - 單擊它,然後運行查詢和比較。 –