Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sql:根据另一个表2中的查找匹配更新表1_Sql_Replace_Matching_Sql Like - Fatal编程技术网

sql:根据另一个表2中的查找匹配更新表1

sql:根据另一个表2中的查找匹配更新表1,sql,replace,matching,sql-like,Sql,Replace,Matching,Sql Like,我有两张桌子: 表1: NULL NULL Cat.XX 23 Cow.XX 87 NULL NULL Tiger.XX 99 Elephant.XX Column1和Column2是分别与column3和column4中的值关联的ID号 表2: 84048713 Cat.XX 23 Blah1 Blah2 Blah3 Blah4 44008714 Elephant.XX 77 Blah1 Blah2 Blah3 Bl

我有两张桌子:

表1:

NULL    NULL    Cat.XX 23   Cow.XX 87
NULL    NULL    Tiger.XX 99 Elephant.XX
Column1和Column2是分别与column3和column4中的值关联的ID号

表2:

84048713    Cat.XX 23   Blah1   Blah2   Blah3   Blah4   
44008714    Elephant.XX 77  Blah1   Blah2   Blah3   Blah4   
64038715    Cow.XX 87   Blah1   Blah2   Blah3   Blah4
34058716    Tiger.XX 99 Blah1   Blah2   Blah3   Blah4
74038717    Zebra.XX 34 Blah1   Blah2   Blah3   Blah4
94098719    Whale.XX 47 Blah1   Blah2   Blah3   Blah4
我想用适当的ID号更新表1中的每一行。结果表1应如下所示:

84048713    64038715    Cat.XX 23   Cow.XX 87
34058716    44008714    Tiger.XX 99 Elephant.XX
select IDs from table2 where 
(select replace("Name", ' ', '') from table2
LIKE
(select replace("Name", ' ', '') from table1)
我尝试了使用select、where和selectreplace的各种组合(我使用replace是因为包含动物名称的字段中有空格)。例如,我尝试了以下方法:

84048713    64038715    Cat.XX 23   Cow.XX 87
34058716    44008714    Tiger.XX 99 Elephant.XX
select IDs from table2 where 
(select replace("Name", ' ', '') from table2
LIKE
(select replace("Name", ' ', '') from table1)
但我得到了以下错误:

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 512,第16级,状态1,第1行
子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时。
我感谢你的帮助。多谢各位

试试这个

update table1 set Column1ID = (select ID from table2 where column2 = table1.column3),Column2ID = (select ID from table2 where column2 = table1.column4)
Update t1
Set t1.col1 = case t1.col3 when t2.col2 then t2.col1 else t1.col1, 
    t1.col2 = case t1.col4 when t2.col2 then t2.col1 else t1.col2
From table1 t1 join table2 t2 
    on t1.col3 = t2.col2 or t1.col4 = t2.col2

将表1集合ID=B.ID、ID2=C.ID从表1更新为左侧 A.TEMPID=B.TEMPID左外部联接表_2上的外部联接表_2作为B 作为A.TEMPUD2上的C=C.TEMPID


我想当你设置column2id时,你的意思是以“where column2=table1.column4”结尾,但是,是的,这就是你的想法。@JohnZ-非常感谢!)@algotr8der-没问题,很乐意为您提供帮助。如果动物名称中的空格是一致的,我的意思是,如果它们在表1和表2中的相同位置上有空格,那么您不需要将它们去掉以获得匹配。你不应该对他们做任何事。上面示例中的“like”与使用equals没有区别,因为字符串中没有通配符。除此之外,我认为约翰兹给出了正确的解决方案。