简体   繁体   English

在SQL中,如何基于另一个字段的值将值从一个表复制到另一个表?

[英]In SQL How do I copy values from one table to another based on another field's value?

Okay I have two tables 好吧,我有两个桌子

VOUCHERT with the following fields VOUCHERT具有以下字段

ACTIVATIONCODE
SERIALNUMBER
VOUCHERDATADBID 
UNAVAILABLEAT   
UNAVAILABLEOPERATORDBID 
AVAILABLEAT 
AVAILABLEOPERATORDBID   
ACTIVATIONCODENEW   
EXT1    
EXT2    
EXT3    
DENOMINATION -- I added this column into the table.

and the second table is VOUCHERDATAT with the following fields 第二个表是VOUCHERDATAT,其中包含以下字段

VOUCHERDATADBID
BATCHID
VALUE
CURRENCY
VOUCHERGROUP
EXPIRYDATE
AGENT
EXT1
EXT2
EXT3

What I want to do is copy the corresponding VALUE from VOUCHERDATAT and put it into DENOMINATION of VOUCHERT. 我要做的是从VOUCHERDATAT复制相应的VALUE并将其放入VOUCHERT的DENOMINATION中。 The linking between the two is VOUCHERDATADBID. 两者之间的链接是VOUCHERDATADBID。 How do I go about it? 我该怎么办?

It is not a 1:1 mapping. 它不是1:1映射。 What I mean is there may be 1000 SERIALNUMBERS with a same VOUCHERDATADBID. 我的意思是可能有1000个SERIALNUMBERS具有相同的VOUCHERDATADBID。 And that VOUCHERDATADBID has only entry in VOUCHERDATAT, hence one value. 而且那个VOUCHERDATADBID在VOUCHERDATAT中只有一个条目,因此是一个值。 Therefore, all serial numbers belonging to a certain VOUCHERDATADBID will have the same value. 因此,属于某个VOUCHERDATADBID的所有序列号将具有相同的值。

Will JOINS work? JOINS会工作吗? What type of JOIN should I use? 我应该使用哪种类型的JOIN? Or is UPDATE table the way to go? 还是UPDATE表走了?

Thanks for the help !! 谢谢您的帮助 !!

Your problem is one of design. 您的问题是设计之一。 None of your tables are in any of the normal forms, not even in the first normal form (1NF). 您的表都没有任何正常形式,甚至没有第一个正常形式(1NF)。 You should not add a column to the VOUCHERT table, but create a new table (pick the name) with the following columns: SERIALNUMBER , VALUE , VOUCHERDATADBID (maybe ACTIVATIONCODE too - need to know the primary key on VOUCHERT to be sure if ACTIVATIONCODE should be included in the new table). 您不应该在VOUCHERT表中添加一列,而应使用以下列创建一个新表(选择名称): SERIALNUMBERVALUEVOUCHERDATADBID (也许也是ACTIVATIONCODE -需要知道VOUCHERT上的主键以确保ACTIVATIONCODE是否应包含在新表中)。 Normalization is the database design process that aims to resolve any possible INSERT / UPDATE / DELETE anomalies. 标准化是旨在解决任何可能的INSERT / UPDATE / DELETE异常的数据库设计过程。 This should solve your INSERT issue. 这应该可以解决您的INSERT问题。

Hope this helps. 希望这可以帮助。

You can do a join between these two tables and you will get a 'view'. 您可以在这两个表之间进行联接,您将获得一个“视图”。 You can update this view like: 您可以像这样更新此视图:

UPDATE (SELECT * 
        FROM VOUCHERT A JOIN VOUCHERDATAT B 
             ON A.VOUCHERDATADBID = B.VOUCHERDATADBID) 
SET DENOMINATION = VALUE;

You may put outer join if you need. 如果需要,可以加入外部联接。

VOUCHERDATADBID MUST BE PRIMARY KEY in VOUCHERDATAT and FOREIGN KEY in VOUCHERT, otherwise you will get an error: VOUCHERDATADBID必须是VOUCHERDATAT中的PRIMARY KEY和VOUCHERT中的FOREIGN KEY,否则您将得到一个错误:

ORA-01779: cannot modify a column which maps to a non key-preserved table
update  (
        select  v.DENOMINATION
        ,       vd.VALUE
        from    VOUCHERT v
        join    VOUCHERDATAT vd
        on      vd.VOUCHERDATADBID = v.VOUCHERDATADBID
        ) t
set     t.DENOMINATION = t.Value

If the voucherdatadbid is not a primary key, this should work: 如果voucherdatadbid不是主键,则应该可以使用:

UPDATE vouchert
   SET denomination =
       (SELECT MAX(value)
          FROM voucherdatat
         WHERE voucherdatadbid = vouchert.voucherdatadbid);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在SQL中将行从一个表复制到另一个表? - How do I copy rows from one table to another in SQL? 在 SQL 中,如何将一整列从一个表复制到另一个没有任何相互列或值的表? - how do i copy one whole column from a table to another without any mutual columns or values in SQL? 如何将 SQL 记录中的一个字段的值更改为另一个表中的值? - How do I change the value of one field in SQL record to a value from another table? 如何基于另一个表中的值设置一个表的字段 - How to set one table's field based on value in another table 如何编写更新SQL来从一个表中复制多个记录以更新另一表上的相应字段? - How do I write an Update SQL to copy multiple records from one table to update the corresponding field on another table? SQL 将值从一个表复制到另一个表 - SQL to copy values from one table to another SQL:如何从一个链接到另一个表中找到描述值? - SQL: How do I find a descript value from one table that's linked to another? 如何根据SQL中另一个字段的值选择一个字段? - How do I select a field based on the value of another field in SQL? 如何将Sql表从一个数据库复制到具有不同字段名称的另一个数据库 - How do you copy Sql table from one database to another database with differ field names 如何根据SQL中另一个字段的值将字段更改为负数 - How do I change a field to a negative based on another field's value in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM