[英]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
表中添加一列,而应使用以下列创建一个新表(选择名称): SERIALNUMBER
, VALUE
, VOUCHERDATADBID
(也许也是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.