简体   繁体   English

用表 B 中的类似列填充表 A 上的现有列的最佳 SQL 查询是什么?

[英]What is the best SQL query to populate an existing column on table A with a similar column from table B?

Let's say I have an existing table A with a column called contact_name and a ID column id as the primary key.假设我有一个现有的表A ,其中包含一个名为contact_name的列和一个 ID 列id作为主键。 All the rows in A have the name value as "NULL" right now. A中的所有行现在的名称值为“NULL”。

Another table B has different columns, but one of which is contact_name , and another is ref_id .另一个表B有不同的列,但其中一个是contact_name ,另一个是ref_id Each ref_id in B corresponds to a value of id in A , and there may be multiple rows in B that share the same value for ref_id (meaning they all correspond to a single entry in A ). B中的每个ref_id对应于A中的一个id值,并且B中可能有多行共享相同的ref_id值(意味着它们都对应于A中的单个条目)。

Let me set up an example:让我举个例子:

Table A表A

id | contact_name
1  | [NULL]
2  | [NULL]

Table B表B

ref_id | contact_name
1      | "John"
2      | "Helen"
2      | "Alex"

Note there are theoretically other values in each table but for the sake of brevity I'm just showing the values I'm interested in using.请注意,理论上每个表中还有其他值,但为了简洁起见,我只显示我有兴趣使用的值。

I want to populate contact_name in table A with the first entry of the corresponding contact_name in B , where B. (first) ref_id = A.id , without adding any rows or editing the rest of the rows in either table.我想用B中相应的contact_name第一个条目填充表A中的contact_name ,其中B. (first) ref_id = A.id ,而不添加任何行或编辑任一表中行的 rest。 That is, I want A in my example to now be:也就是说,我希望示例中的 A现在是:

id | contact_name
1  | "John"
2  | "Helen"

Again, note how the first contact_name value, "Helen", in B is selected, not any other subsequent one, like "Alex".再次注意如何选择B中的第一个contact_name值“Helen”,而不是任何其他后续值,如“Alex”。

The MERGE function is made for this. MERGE function 就是为此而制作的。 Give it a try:试一试:

MERGE INTO TABLEA a USING (
    SELECT b.REF_ID, b.CONTACT_NAME
    FROM TABLEB) b
ON (a.ID = b.REF_ID)
WHEN MATCHED THEN
    UPDATE SET
        a.CONTACT_NAME = b.CONTACT_NAME
    WHERE a.CONTACT_NAME IS null;

暂无
暂无

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

相关问题 从另一个表填充SQL表列 - Populate SQL table column from another table SQL:将表 B 中的列添加到表 A - SQL: Adding column from table B to table A 如何查询表 A 中的所有记录和表 B 中的类似匹配项作为不同列中的布尔值 - How do I query all records from table A and similar matches from table B as a boolean in a different column 将另一个表中的列添加到现有的SQL /查询语句中 - Adding a column from another table into existing SQL/query statement 编写SQL查询以查找表X中列A中不存在于表Y的列B中的那些值 - Write a SQL query to find those values from column A in table X that are NOT present in column B of table Y 使用另一个表中的值填充SQL中的列 - Populate a column in SQL with values from another table 在保持关系的同时将现有表中的几列分离到新表中的最佳方法是什么? - What is the best way to separate out a few column from an existing table into a new table while maintaining relationship? SQL 查询从较小的表中提取数据以填充较大表上的列 - SQL query to Extract data from smaller table to populate a column on the bigger table 当列的值不在表B中时,SQL查询从表A中获取行,并计算将存储在表B中的内容 - SQL query get row from table A when it's column value is not in table B and calculate something that will be stored in table B SQL触发器,用于将值从表A-列A复制到表B-列A IF值在表B中是唯一的 - SQL Trigger to copy value from Table A - Column A to Table B - Column A IF Value is unique in Table B
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM