根据同一个表中的字段更新表中的字段
大家好,
我希望从同一个表中的一行更新表中的一行,
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您非常接近,您只需指定要从哪个表中选择作为子查询的一部分...
即使“your_table”是 INVENTORY,您仍然需要在其中指定它。
唯一变得“棘手”的情况是当您从要更新的同一个表中进行选择时,以及当您需要从 SELECT 语句中更新的记录中获取值时。在这种情况下,您需要使用别名来区分两个引用。例如...
You're very close, you just need to specify which table you're selecting from as part of your sub-query...
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...
这会将
INVENTORY
中所有“库存”记录的价格更新为相应“销售”记录中的价格。如果您希望更新单个价格,请将
WHERE
更改为如下所示:或这样:
注意:使用的 FROM 语法基于 此文档页面。
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:or this:
Note: The FROM syntax used is based on this doc page.