比较多个Mysql表以查找修改记录

发布于 2024-10-27 10:37:55 字数 478 浏览 2 评论 0原文

我有三个表作为 SQLITE3 转储。

产品、税收、库存

从这些表中准备一个 csv 文件以导入到 magento 产品部分。那么这部分就结束了。

现在必须运行每日脚本来更新库存、税费、修改的产品详细信息(SKU 在任何情况下都不会更改)。

在本次更新部分。 例如,我有 12000 条记录,因为最后一天的数据库中的库存表、税务表、产品表中可能只有 400 或 1000 条记录发生变化/修改/更新。其余行将保持不变。

因此,我将最后几天的表格添加为 old_products,old_tax,old_inventory

并尝试编写 sql 查询以从两组表中仅提取修改的记录

(注意:两组表都在同一数据库中可用。)

请任何人帮助我如何编写 sql 查询来提取通过比较两组表来修改记录。

I have three tables as a SQLITE3 Dump.

products,tax,inventory

From these tables preparing a csv file to import into magento products section. SO that part is over.

Now have to run a daily script which updates the inventory,tax, modified product details(SKU will not change at any case).

In this update section.
For example, i have 12000 records, in that only may be 400 or 1000 records varied/modified/updated in inventory table,tax table,products table from the last day's Database. The remaining rows will remain same.

So i added the last days tables as old_products,old_tax,old_inventory.

And tried to write sql query to extract only modified records from the two set's of tables

(Note: Both sets of tables available in Same Db.)

Please can anyone help me how to write a sql query to extract the modified records by comparing two sets of tables.

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

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

发布评论

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

评论(2

在风中等你 2024-11-03 10:37:55

我可能会改变这个模式。

我将有一个表,用于存储上次将数据导出到 CSV 时的日期:

CREATE TABLE CSVLastExported (
  ProductsExportDate datetime,
  TaxExportDate datetime,
  InventoryExportDate datetime
)

然后我可以在三个表中的每一个中都有一个时间戳列,用于保存上次修改时间戳。我会有一个更新该专栏的触发器。

然后,查询修改后的数据以进行另一次导出就像将 LastModified 列与 CSVLastExported 中的相应列进行比较一样简单:

SELECT p.*
FROM products p
  INNER JOIN CSVLastExport e
    ON p.LastModified > e.ProductsExportDate

I would probably change this schema.

I would have a table where I would store the date(s) when I last exported the data to CSV(s):

CREATE TABLE CSVLastExported (
  ProductsExportDate datetime,
  TaxExportDate datetime,
  InventoryExportDate datetime
)

And I could then have a timestamp column in each of the three tables which would hold the last modification timestamp. I would have a trigger for updating that column.

Querying the modified data for another export would then be as easy as comparing the LastModified column against the corresponding column in CSVLastExported:

SELECT p.*
FROM products p
  INNER JOIN CSVLastExport e
    ON p.LastModified > e.ProductsExportDate
烟雨扶苏 2024-11-03 10:37:55

这为您提供了所有修改(和产品):

 select * from products p inner join old_products o on p.id = o.id

这是修改后的产品:

 select * from products where id in (select id old_products)

this gives you all modifications (and the product):

 select * from products p inner join old_products o on p.id = o.id

and this the modificated products:

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