两个SQL Server查询之间的区别?
我想知道两个SQL Server查询之间的区别吗?
表:
//i.sstatic.net/otowo.png“ alt =“在此处输入图像说明”>
我想做的是加入上面的表并应用过滤器。这样的过滤器之一是基于另一个表。
查询#1
UPDATE STORE_ITEM
SET user_bit_5 = '1'
FROM STORE_ITEM
INNER JOIN tag_demand ON STORE_ITEM.item_id = tag_demand.item_id
AND STORE_ITEM.store_nbr = tag_demand.store_nbr
WHERE tag_demand.print_dt = '2022-04-11'
AND tag_demand.STORE_NBR IN (SELECT store_nbr FROM store WHERE division_id = 'XYZ')
AND user_bit_5 = '0';
查询#2
UPDATE STORE_ITEM
SET user_bit_5 = '1'
FROM STORE_ITEM
INNER JOIN tag_demand ON STORE_ITEM.item_id = tag_demand.item_id
AND STORE_ITEM.store_nbr = tag_demand.store_nbr
AND tag_demand.print_dt = '2022-04-11'
AND tag_demand.STORE_NBR IN (SELECT store_nbr FROM store WHERE division_id = 'XYZ')
WHERE user_bit_5 = '0';
第一个查询在联接上具有更多条件,而第二个查询将它们放在WHERE子句上。
I want to know the difference between two SQL Server queries?
Tables:
What I am want to do is that join the above tables and apply filters. One of such filter is based on another table.
Query#1
UPDATE STORE_ITEM
SET user_bit_5 = '1'
FROM STORE_ITEM
INNER JOIN tag_demand ON STORE_ITEM.item_id = tag_demand.item_id
AND STORE_ITEM.store_nbr = tag_demand.store_nbr
WHERE tag_demand.print_dt = '2022-04-11'
AND tag_demand.STORE_NBR IN (SELECT store_nbr FROM store WHERE division_id = 'XYZ')
AND user_bit_5 = '0';
Query#2
UPDATE STORE_ITEM
SET user_bit_5 = '1'
FROM STORE_ITEM
INNER JOIN tag_demand ON STORE_ITEM.item_id = tag_demand.item_id
AND STORE_ITEM.store_nbr = tag_demand.store_nbr
AND tag_demand.print_dt = '2022-04-11'
AND tag_demand.STORE_NBR IN (SELECT store_nbr FROM store WHERE division_id = 'XYZ')
WHERE user_bit_5 = '0';
First query has more conditions on the join while the second query has them on the where clause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您向我们展示的特定查询没有差异。 SQL Server的查询计划器可能会使用相似的(即使不是相同)的操作来满足两者。
尽管如此,您的第二个查询模式(与
左JOIN
一起使用时,并且使用Select 不更新)很常见。使用左JOIN
像您的第二个这样的查询总是会从store_item
中返回至少一行,即使它没有从tag_demand
中找到匹配的行。(如果您使用
左JOIN
而不是普通内在
在第二个查询中,则将在store_item
表中更新所有行。)No differences in the particular queries you showed us. SQL Server's query planner will probably use similar, if not identical, operations to satisfy both.
Still, your second query pattern, when used with
LEFT JOIN
, and with SELECT not UPDATE, is quite common. WithLEFT JOIN
a query like your second one would always return at least one row fromSTORE_ITEM
, even if it found no matching rows fromtag_demand
.(If you use
LEFT JOIN
instead of ordinaryINNER JOIN
in your second query, you will update all rows in yourSTORE_ITEM
table.)