追加新数据并通过插入另一个数据更新表中的现有数据

发布于 2025-01-17 06:34:11 字数 1212 浏览 5 评论 0原文

在 SQL 中,我有一个包含 3 列的表,该表每天都会刷新。我想将“表a”数据附加到“表b”。我需要确保按日期没有重复的行,而且销售计数会增加,销售计数会更新,而不是放入新行。

表 a

日期idsale_count
12/20/2117
12/20/2123
12/21/2122

表 b 预更新

日期idsale_count
12/20/2115
12/20/2123

表 b发布更新

日期IDsale_count
12/20/2117
12/20/2123
12/21/2122

到目前为止我的代码仍然允许重复行

INSERT INTO tableB
SELECT
  a.[date]
  ,a.id
  ,a.sale_count
FROM tableA a
Left outer join tableB b
on a.[date] = b.[date] 
and a.id = b.id
and a.sale_count = b.sale_count
where b.[date] IS NULL
AND b.sale_count is NULL

In SQL, I have a table with 3 columns that is refreshed through the day. I want to append "table a" data to "table b". I need to ensure the there are no duplicate rows by date, but also that is the sale_count in creases that the sale count is updated and not put in a a new row.

table a

dateidsale_count
12/20/2117
12/20/2123
12/21/2122

table b pre-update

dateidsale_count
12/20/2115
12/20/2123

table b post update

dateidsale_count
12/20/2117
12/20/2123
12/21/2122

code I have so far that is still allowing duplicate rows

INSERT INTO tableB
SELECT
  a.[date]
  ,a.id
  ,a.sale_count
FROM tableA a
Left outer join tableB b
on a.[date] = b.[date] 
and a.id = b.id
and a.sale_count = b.sale_count
where b.[date] IS NULL
AND b.sale_count is NULL

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文