简体   繁体   English

使用值“ Y”更新表“ A”中的行,以匹配来自其他表“ B”的记录

[英]Update rows in table 'A' with value 'Y', for matching records from a different table 'B"

TESTDTA is the test database.
F41= First table
F42=Second table

Data from F41 TABLE 来自F41表格的数据

FLAG    STORE   NAME    NUMBER
S         1      A       A1
S         2      B       B2
S         3      C       C3

Data from F42 TABLE 来自F42表格的数据

STORE   NAME    NUMBER
1        A        A1
2        B        B2
3        C        C3
4        D        D4

I need to update values for the column "FLAG" in the tabel "F41" to value "P" if there is a matching record in the table "F42" . 如果表“ F42”中有匹配的记录,则需要将表“ F41”中列“ FLAG”的值更新为值“ P”。

I tried below SQL. 我尝试下面的SQL。 But it has syntax error. 但是它有语法错误。

UPDATE TESTDATA.F41,TESTDATA.F42 SET F41.FLAG='P' 
WHERE F41.NAME=F42.NAME AND F41.NUMBER=F42.NUMBER

Can anyone help me to write this SQL? 谁能帮我写这个SQL吗?

Thanks in advance for your help 在此先感谢您的帮助

Perhaps a little cleaner 也许有点清洁

UPDATE F41 SET FLAG='P' 
 FROM  F41 A
 JOIN  F42 B on A.NAME=B.NAME AND A.NUMBER=B.NUMBER

if your DBMS is Oracle and If by "matching records" you mean all column values in F41 match F42 then this might answer your problem: 如果您的DBMS是Oracle,并且如果通过“匹配记录”来表示F41中的所有列值都匹配F42,那么这可能会回答您的问题:

DDL: DDL:

create tabLe F41
(
    FLAG        varchar2(10)
   ,STORE_x       number
   ,NAME_x        varchar2(10)
   ,NUMBER_x      varchar2(10)
);

create tabLe F42
(
    STORE_x       number
   ,NAME_x        varchar2(10)
   ,NUMBER_x      varchar2(10)
);

Note: Added _x to the Columns STORE, NAME, NUMBER because those are reserved words in some DBMS. 注意:在列STORE,NAME和NUMBER中添加了_x,因为它们是某些DBMS中的保留字。

DML: DML:

insert into F41 (FLAG, STORE_x, NAME_x, NUMBER_x) values ('S', 1, 'A', 'A1');
insert into F41 (FLAG, STORE_x, NAME_x, NUMBER_x) values ('S', 2, 'B', 'B2');
insert into F41 (FLAG, STORE_x, NAME_x, NUMBER_x) values ('S', 3, 'C', 'C3');

insert into F42 (STORE_x, NAME_x, NUMBER_x) values (1, 'A', 'A1');
insert into F42 (STORE_x, NAME_x, NUMBER_x) values (2, 'B', 'B2');
insert into F42 (STORE_x, NAME_x, NUMBER_x) values (3, 'C', 'C3');
insert into F42 (STORE_x, NAME_x, NUMBER_x) values (4, 'D', 'D4');

Update Statement Using Exists: 使用现有更新语句:

update  F41 t1
set     flag = 'P'
WHERE   EXISTS (SELECT  1
                FROM    F42 t2
                WHERE   t2.STORE_X  = t1.STORE_X
                AND     t2.NAME_X   = t1.NAME_X
                AND     t2.NUMBER_X = t1.NUMBER_X);    

Hope this Helps! 希望这可以帮助!

You can do this with nothing more fancy than a WHERE clause: 您可以通过WHERE子句完成此操作:

UPDATE TESTDATA.F41
    SET FLAG = 'P' 
    WHERE EXISTS (SELECT 1
                  FROM TESTDATE.F42
                  WHERE F41.NAME = F42.NAME AND F41.NUMBER = F42.NUMBER
                 );

This is standard SQL and should work in any database. 这是标准的SQL,可以在任何数据库中使用。

UPDATE TESTDATA.F41 SET F41.FLAG='P' 
FROM TESTDATA.F42
WHERE F41.NAME = F42.NAME AND F41.NUMBER = F42.NUMBER
UPDATE TESTDATA.F41
SET FLAG = 'P' 
WHERE EXISTS (SELECT 1
              FROM TESTDATE.F42
              WHERE F41.NAME = F42.NAME AND F41.NUMBER = F42.NUMBER
             );

暂无
暂无

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

相关问题 从表A中删除记录,其中表B中的字段x = y - Delete records from table A where field x in Table B = y 从表中选择与其他表记录匹配的行 - Select rows from a table matching other table records Sql Join从A表中选择记录和从B表中选择符合条件的记录 - Sql Join selecting records from A table and selecting matching records with a condition from B table 显示具有来自不同表的匹配数据的 SQL 表记录 - Display SQL Table records that have matching data from different table 根据表A中的数组值从表B中选择多行 - Select Multi rows from table B according to array value in table A 如何从table_a中选择记录,如果table_b中与table_a相连的记录有一定的值 - How to select records from table_a, if in table_b records connected with table_a has a certain value 在表b中插入丢失的行并更新表a - Insert missing rows in table b and update table a 将表A和表B之间的不同行插入表B - Insert different rows between Table A and Table B into Table B Hive:内部连接从“table b”更新“table a”,其中“table b”中的值与“table a”不同? 创建最后一个视图 (a+b) - Hive : inner join to update "table a" from "table b" where values in "table b" is different from "table a" ? create a last view (a+b) 如果列表包含表b另一列的子字符串,则用表b的列值更新列表 - update column table with value of column from table b if it contains substring from another column from table b
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM