两个SQL Server查询之间的区别?

发布于 2025-01-21 08:41:13 字数 1585 浏览 0 评论 0原文

我想知道两个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:

enter image description here

enter image description here

What I am want to do is that join the above tables and apply filters. One of such filter is based on another table.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2025-01-28 08:41:13

您向我们展示的特定查询没有差异。 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. With LEFT JOIN a query like your second one would always return at least one row from STORE_ITEM, even if it found no matching rows from tag_demand.

(If you use LEFT JOIN instead of ordinary INNER JOIN in your second query, you will update all rows in your STORE_ITEM table.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文