gpt4 book ai didi

sql - 需要一个专注于表组合的 SQL 语句,但条目始终具有唯一 ID

转载 作者:IT王子 更新时间:2023-10-29 06:29:07 25 4
gpt4 key购买 nike

我需要SQL代码来解决表组合问题,如下所述:

表旧数据:表旧

    name     version    status    lastupdate      ID
A 0.1 on 6/8/2010 1
B 0.1 on 6/8/2010 2
C 0.1 on 6/8/2010 3
D 0.1 on 6/8/2010 4
E 0.1 on 6/8/2010 5
F 0.1 on 6/8/2010 6
G 0.1 on 6/8/2010 7

表新增数据:表新增

    name     version    status    lastupdate     ID         
A 0.1 on 6/18/2010
#B entry deleted
C 0.3 on 6/18/2010 #version_updated
C1 0.1 on 6/18/2010 #new_added
D 0.1 on 6/18/2010
E 0.1 off 6/18/2010 #status_updated
F 0.1 on 6/18/2010
G 0.1 on 6/18/2010
H 0.1 on 6/18/2010 #new_added
H1 0.1 on 6/18/2010 #new_added

新数据与旧数据的区别:

B 条目已删除

C 入门版本更新

E 条目状态已更新

新增C1/H/H1条目

我想要的是始终在旧数据表中保留 ID - 名称映射关系,无论数据后来如何更改,也就是名称始终具有与之绑定(bind)的唯一 ID 号。

如果条目有更新,则更新数据,如果条目是新添加的,则插入到表中,然后赋予新分配的唯一ID。如果该条目已删除,请删除该条目并且以后不再使用该 ID。

但是,我只能使用带有简单 select 或 update 语句的 SQL,那么编写这样的代码对我来说可能太难了,那么我希望有专业知识的人可以指导,不需要详细说明 SQL 变体的不同,一个标准sql代码作为示例就足够了。

提前致谢!

Rgs

KC

========我在这里列出了我的草稿sql,但不确定它是否有效,请有经验的人评论,谢谢!

1.复制旧表作为存储更新的tmp

创建表 tmp 为从旧的中选择 *

2.更新到新旧表中“名称”相同的tmp

更新tmp其中名称在(从新选择名称)

3.insert different "name"(old vs new) into tmp and assign new ID

插入tmp(名称版本状态lastupdate ID)设置 idvar = max(从 tmp 中选择 max(id))+ 1选择 * 来自 (select new.name new.version new.status new.lastupdate new.ID 从旧的,新的 其中旧名 <> 新名)

4。从tmp表中删除删除的条目(如B)

从 tmp 中删除在哪里(选择???)

最佳答案

您从未提及您使用的是什么 DBMS,但如果您使用的是 SQL Server,一个非常好的 SQL MERGE 语句。请参阅:http://www.mssqltips.com/tip.asp?tip=1704

The MERGE statement basically works as separate insert, update, and delete statements all within the same statement. You specify a "Source" record set and a "Target" table, and the join between the two. You then specify the type of data modification that is to occur when the records between the two data are matched or are not matched. MERGE is very useful, especially when it comes to loading data warehouse tables, which can be very large and require specific actions to be taken when rows are or are not present.

例子:

MERGE Products AS TARGET
USING UpdatedProducts AS SOURCE
ON (TARGET.ProductID = SOURCE.ProductID)
--When records are matched, update
--the records if there is any change
WHEN MATCHED AND TARGET.ProductName <> SOURCE.ProductName
OR TARGET.Rate <> SOURCE.Rate THEN
UPDATE SET TARGET.ProductName = SOURCE.ProductName,
TARGET.Rate = SOURCE.Rate
--When no records are matched, insert
--the incoming records from source
--table to target table
WHEN NOT MATCHED BY TARGET THEN
INSERT (ProductID, ProductName, Rate)
VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate)
--When there is a row that exists in target table and
--same record does not exist in source table
--then delete this record from target table
WHEN NOT MATCHED BY SOURCE THEN
DELETE
--$action specifies a column of type nvarchar(10)
--in the OUTPUT clause that returns one of three
--values for each row: 'INSERT', 'UPDATE', or 'DELETE',
--according to the action that was performed on that row
OUTPUT $action,
DELETED.ProductID AS TargetProductID,
DELETED.ProductName AS TargetProductName,
DELETED.Rate AS TargetRate,
INSERTED.ProductID AS SourceProductID,
INSERTED.ProductName AS SourceProductName,
INSERTED.Rate AS SourceRate;
SELECT @@ROWCOUNT;
GO

关于sql - 需要一个专注于表组合的 SQL 语句,但条目始终具有唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2994541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com