简体   繁体   English

如何仅将唯一记录从另一个表导入到一个表中?

[英]How do I only import unique records into a table from another table?

I am trying to update a table in SQL Server.我正在尝试更新 SQL 服务器中的表。 Table is called table1 with columns(code, desc).表称为 table1 列(代码,描述)。 I want to update table1 with unique records from table2(code, desc).我想用 table2(code, desc) 中的唯一记录更新 table1。 table2 is a copy of table1 but it contains records that are not present in table1. table2 是 table1 的副本,但它包含 table1 中不存在的记录。

Sounds like you want an INSERT听起来你想要一个INSERT

Something like this should work:像这样的东西应该工作:

INSERT INTO table1 (code, desc)
SELECT t2.code, t2.desc
FROM table2 t2
LEFT JOIN table1 t1 on t2.code = t1.code and t1.desc = t2.desc
WHERE t1.code is null --Ignore records that already exist in table1

... Adjust join clause accordingly. ... 相应地调整连接子句。

To update table1.desc with values from matching rows in table2 simply do:要使用table2中匹配行的值更新table1.desc ,只需执行以下操作:

update t1 set 
  t1.desc = t2.desc
from table1 t1
join table2 t2 on t2.code = t1.code;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一个表中为另一个表中的每个唯一记录选择 100 条记录 - How do I select 100 records from one table for each unique record from another 如何仅在表中选择具有另一个表中所有值的记录? - How do I only select records in a table that have all the values from another table? 如何根据另一个表从连接表复制记录 - How do I copy records from and to a join table based on another 如何显示SQL表中的所有记录并将这些记录与另一个表匹配? - How do I display all records from a SQL table and match those records to another table? 我如何获取一个表中另一个表中不存在的记录? - How do I get a records for a table that do not exist in another table? 如何在 MySQL 中将唯一记录从一个表插入到另一个表 - How to insert unique records from one table to another in MySQL 如何从一个表中获取记录数,并从另一个表中获取详细信息 - How do I get a count of records from one table with detail from another table 如何插入从另一个表中提取的表记录 - How to do INSERT into a table records extracted from another table 如何显示一个表中不存在于另一表中的记录 - How to show records from one table that do not exist in another table 如何从另一个表中缺少的一个表中添加记录:MS Access - How do I add records from one table missing in another table : MS Access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM