更新数据集

发布于 2024-11-15 01:55:56 字数 187 浏览 7 评论 0原文

假设我有 2 个数据集:A 和 B,它们具有相同的列。我想要获得“距离路径” - 需要完成最少的操作,才能将数据集 A 更改为 B。

我可以假设,A 和 B 只包含很少的差异,并且我正在寻找更好的东西,然后:

  • 删除所有A的元素
  • 将B的所有元素插入到A中,

这如何归档?

Lets say I have 2 datasets: A and B, that have the same columns. I want to get the 'distance path' - minimal operations needed to be done, to change datasets A to B.

I can assume, that A and B contain only in few differences and I'm looking for something better, then:

  • remove all A's elements
  • insert all B's elements into A

How can this be archived?

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

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

发布评论

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

评论(2

隐诗 2024-11-22 01:55:56

如果您知道如何使用 SQL:

SELECT a.* FROM a 
LEFT JOIN b on (a.field1 = b.field1 AND a.field2 = b.field2 AND ....)
WHERE b.field1 IS NULL

将为您提供 A 中 B 中没有的所有字段。

现在执行

INSERT INTO b
  SELECT a.* FROM a 
  LEFT JOIN b on (a.field1 = b.field1 AND a.field2 = b.field2 AND ....)
  WHERE b.field1 IS NULL

,然后执行(或不执行,具体取决于您的需要)。

DELETE b FROM b
LEFT JOIN a ON (a.field1 = b.field1 and a.field2 = b.field2 AND ...)
a.field1 IS NULL

现在表ab 将是相同的。

像这样的 Delphi 代码应该可以解决问题,但确切的代码取决于您的数据库和使用的查询组件。

procedure TForm1.equalize(A, B: TDataset);
var
  tablenameA: string;
  tablenameB: string;
  MyQuery: TQuery;
begin
  tablenameA:= IProviderSupport(A).PSGetTableName;
  tablenameB:= IProviderSupport(B).PSGetTableName;
  MyQuery:= TQuery.Create(self);
  MyQuery.Database:= .....
  MyQuery.SQL.Text:= ' INSERT INTO '+tablenameA+' .....
  MyQuery.ExecSQL;
end;

If you know how to use SQL:

SELECT a.* FROM a 
LEFT JOIN b on (a.field1 = b.field1 AND a.field2 = b.field2 AND ....)
WHERE b.field1 IS NULL

Will give you all fields in A that are not in B.

Now do

INSERT INTO b
  SELECT a.* FROM a 
  LEFT JOIN b on (a.field1 = b.field1 AND a.field2 = b.field2 AND ....)
  WHERE b.field1 IS NULL

And then do (or don't do depending on your needs).

DELETE b FROM b
LEFT JOIN a ON (a.field1 = b.field1 and a.field2 = b.field2 AND ...)
a.field1 IS NULL

Now table a and b will be the same.

Delphi code like this should do the trick, but the exact code depends on your database and the query components used.

procedure TForm1.equalize(A, B: TDataset);
var
  tablenameA: string;
  tablenameB: string;
  MyQuery: TQuery;
begin
  tablenameA:= IProviderSupport(A).PSGetTableName;
  tablenameB:= IProviderSupport(B).PSGetTableName;
  MyQuery:= TQuery.Create(self);
  MyQuery.Database:= .....
  MyQuery.SQL.Text:= ' INSERT INTO '+tablenameA+' .....
  MyQuery.ExecSQL;
end;
孤城病女 2024-11-22 01:55:56

如果你想知道如何将A转化为B,你需要知道A和B之间的区别是什么。有一个非常简单的通用算法,尽管它可能会更复杂,具体取决于数据集中有多少字段以及 A 到 B 中的字段数量可能不同。但总体思路是:对

  • 两个数据集进行排序。确保按照完全相同的标准进行排序。 (此时,如果它们都包含相同的数据元素集,则如果并排查看,它们的两个列表的内容将是相同的。)
  • 从两者的第一项开始
    数据集。通过可返回小于等于大于的比较来比较这两个项目。
    • 如果 A < B,注意差异并移至 A 的下一行。
    • 如果A> B,注意差异并移至 B 的下一行。
    • 如果 A = B,则将两个数据集前进到下一行。
  • 重复比较并前进步骤,直到两个数据集都达到 EOF。

完成后,您将获得 A 和 B 之间差异的完整列表,这将使将 A 转换为 B 所需的步骤易于计算。

If you want to find out how to transform A into B, you need to know what the difference between A and B is. There's a very simple general algorithm for that, though it might be more complicated depending on how many fields are in your dataset and how many of them can be different from A to B. But here's the general idea:

  • Sort both datasets. Make sure to sort on the exact same criteria. (At this point, if they both contained the same set of data elements, the two lists of their contents would be identical if viewed side by side.)
  • Begin with the first item of both
    datasets. Compare the two items with a comparison that can return less than, equal or greater than.
    • If A < B, note the difference and move to the next row of A.
    • If A > B, note the difference and move to the next row of B.
    • If A = B, advance both datasets to the next row.
  • Repeat the compare-and-advance step until you've reached EOF on both datasets.

Once you're done, you'll have a full list of differences between A and B, which will make the steps needed to transform A to B simple to calculate.

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