根据同一个表中的字段更新表中的字段

发布于 2024-10-19 16:06:29 字数 370 浏览 6 评论 0原文

大家好,

我希望从同一个表中的一行更新表中的一行,

我有一个名为 INVENTORY 的表。它存储产品的价格。 我有相关的销售代码和库存代码。

我希望将所有定价从销售代码转移到库存代码。

我希望做这样的事情:

update INVENTORY 
set PRICE = (SELECT PRICE WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE 
CODE = "SOME STOCK CODE"

销售代码看起来像这样“U_12345”,其等效的股票代码看起来像“S_​​12345”

谢谢

HI ALL

I wish to update a row within my table from a row within that same table,

I have a table called INVENTORY. it stores PRICES for products.
I have SALES codes and I have STOCK codes which are related.

I wish to transfer all the PRICING from the SALES codes to the STOCK codes.

I wish to do something like this:

update INVENTORY 
set PRICE = (SELECT PRICE WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE 
CODE = "SOME STOCK CODE"

a SALES CODE would look like this "U_12345", its STOCK code equivalent would look like "S_12345"

thanks

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

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

发布评论

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

评论(2

千寻… 2024-10-26 16:06:29

您非常接近,您只需指定要从哪个表中选择作为子查询的一部分...

update INVENTORY 
set PRICE = (SELECT PRICE FROM your_table WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE 
CODE = "SOME STOCK CODE"

即使“your_table”是 INVENTORY,您仍然需要在其中指定它。

唯一变得“棘手”的情况是当您从要更新的同一个表中进行选择时,以及当您需要从 SELECT 语句中更新的记录中获取值时。在这种情况下,您需要使用别名来区分两个引用。例如...

update INVENTORY 
set PRICE = (SELECT PRICE FROM INVENTORY AS [new_price] WHERE [new_price].CODE = INVENTORY.NEW_CODE)
WHERE 
CODE = "SOME STOCK CODE"

You're very close, you just need to specify which table you're selecting from as part of your sub-query...

update INVENTORY 
set PRICE = (SELECT PRICE FROM your_table WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE 
CODE = "SOME STOCK CODE"

Even if "your_table" is INVENTORY, you still need to specify it in there.

The only time it gets 'tricky' is when your selecting from the same table that you're update, AND when you need a value from the updated record in the SELECT statement. In that case you need to differentiate between the two references, using an alias. For example...

update INVENTORY 
set PRICE = (SELECT PRICE FROM INVENTORY AS [new_price] WHERE [new_price].CODE = INVENTORY.NEW_CODE)
WHERE 
CODE = "SOME STOCK CODE"
贪恋 2024-10-26 16:06:29
UPDATE INVENTORY
SET PRICE = i_sales.PRICE
FROM INVENTORY, INVENTORY i_sales
WHERE INVENTORY.CODE LIKE 'S\_%'
  AND i_sales.CODE = REPLACE(INVENTORY.Code, 'S', 'U')

这会将 INVENTORY 中所有“库存”记录的价格更新为相应“销售”记录中的价格。

如果您希望更新单个价格,请将 WHERE 更改为如下所示:

WHERE i_stock.CODE = 'S_12345'

或这样:

WHERE i_stock.CODE IN ('S_12345', 'S_12346')

注意:使用的 FROM 语法基于 文档页面。

UPDATE INVENTORY
SET PRICE = i_sales.PRICE
FROM INVENTORY, INVENTORY i_sales
WHERE INVENTORY.CODE LIKE 'S\_%'
  AND i_sales.CODE = REPLACE(INVENTORY.Code, 'S', 'U')

This updates prices for all 'stock' records in INVENTORY with prices from the corresponding 'sales' records.

If you would prefer to update individual prices, change WHERE to something like this:

WHERE i_stock.CODE = 'S_12345'

or this:

WHERE i_stock.CODE IN ('S_12345', 'S_12346')

Note: The FROM syntax used is based on this doc page.

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