简体   繁体   English

SQL表联接问题

[英]SQL Tables Joining issues

I have two tables, Table 1 and 2. I want to update information at Table 1 based on Table 2. For example, correct AA in table 1 from 10 to 30. 我有两个表,表1和2。我想基于表2更新表1的信息。例如,将表1中的AA正确地从10改为30。

What quires should I write? 我应该写些什么?

Thanks, 谢谢,

在此处输入图片说明

you don't want to do a join from what I can tell, but instead you should do an update. 您不想根据我的判断进行联接,而是应该进行更新。 It does get a bit more complicated when you're using data from another table instead of feeding raw data straight into the query. 当您使用来自另一个表的数据而不是将原始数据直接输入到查询中时,它的确变得更加复杂。

UPDATE Table1 t1,
     Table2 t2
SET t1.num = t2.num
WHERE t1.name == t2.name;

not the exact code of course because the question and tables are somewhat vague but I believe this is the right direction. 当然不是确切的代码,因为问题和表格有些含糊,但我认为这是正确的方向。

Try an update with a join. 尝试使用联接进行更新。

UPDATE TABLE1 a JOIN TABLE2 b ON a.join_colA = b.join_colA UPDATE TABLE1 a JOIN TABLE2 b在a.join_colA = b.join_colA上
SET a.numberColumn = b.numberColumn SET a.numberColumn = b.numberColumn

Here column join_colA is your first column numberColumn would be your other value column. 在这里,join_colA列是您的第一列numberColumn,将是您的其他值列。

I would solve this problem in 3 steps 我将分三步解决这个问题

Step 1: Join the tables 步骤1:加入表格
Step 2: Update the null values from table 1 步骤2:更新表1中的空值
Step 3: Drop the unnecessary column 步骤3:删除不必要的列

Select A.*, B.Column2 as column3 from A
left join B
on A.Column1=B.Column1


update table1
set column3= column2

alter table table1
drop column column2

Figure out the syntax errors that you may encounter 找出可能遇到的语法错误

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM