SQL 更新空表值

发布于 2024-12-10 00:14:36 字数 837 浏览 0 评论 0原文

我正在使用两个表,表一是part_order

ONum    PNum    NumOrdered  QuotPrice
2236    BZ66    1   
2236    AZ52    10          29.90
2237    CA14    3   
2240    CB03    1   

,表二是part,看起来

PNum    Description OnHand  Warehouse   UnitPrice
AZ52    Skates          20  2           36.95
BZ66    Washer          5   1           465.50
CA14    Skillet         13  1           28.99
CB03    Bike            44  3           195.50

我正在尝试用unitprice 值更新我的quotprice 列(如果它为空)。这意味着 29.90 应保留,但其他应更新。

我输入:

 update part_order
    set quotprice = (select part.unitprice
    from part
    where part.pnum = part_order.pnum
    and quotprice is null);

除了切换值之外它还可以工作。我的 29.90 变成 null,其他 3 个值被更新。如果我再次输入,其他 3 个值将返回 null,而第二个值将填充 36.95。我想知道为什么它会更改值而不是仅检查空值。

I am working with two tables, table one is part_order looks like

ONum    PNum    NumOrdered  QuotPrice
2236    BZ66    1   
2236    AZ52    10          29.90
2237    CA14    3   
2240    CB03    1   

and table two is part and looks like

PNum    Description OnHand  Warehouse   UnitPrice
AZ52    Skates          20  2           36.95
BZ66    Washer          5   1           465.50
CA14    Skillet         13  1           28.99
CB03    Bike            44  3           195.50

I'm attempting to update my quotprice column with unitprice values, if it is null. Meaning the 29.90 should stay but the others should be updated.

I type in:

 update part_order
    set quotprice = (select part.unitprice
    from part
    where part.pnum = part_order.pnum
    and quotprice is null);

And it works aside from it switches the values. My 29.90 turns to null, and the other 3 values are updated. If I type it again, the other 3 values go back to null and the 2nd value is filled with 36.95. I am wondering why it changes the values instead of only checking for the null values.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

挽清梦 2024-12-17 00:14:36
UPDATE po
SET po.QuotPrice = p.UnitPrice
FROM part_order AS po
INNER JOIN part AS p ON po.pnum = p.pnum
WHERE po.QuotPrice IS NULL
UPDATE po
SET po.QuotPrice = p.UnitPrice
FROM part_order AS po
INNER JOIN part AS p ON po.pnum = p.pnum
WHERE po.QuotPrice IS NULL
紫南 2024-12-17 00:14:36
 update part_order
   set quotprice = (select part.unitprice
                    from part
                    where part.pnum = part_order.pnum)
   WHERE quotprice IS NULL;
 update part_order
   set quotprice = (select part.unitprice
                    from part
                    where part.pnum = part_order.pnum)
   WHERE quotprice IS NULL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文