gpt4 book ai didi

mysql - 使用来自不同表的修改后的数据填充表。 (SQL)

转载 作者:行者123 更新时间:2023-11-29 06:35:30 25 4
gpt4 key购买 nike

我是 SQL 新手,所以如果这是一个简单的问题,我很抱歉。这是它最简单的版本(看起来与 this question 非常相似):

我有两个表,Table1 和 Table2。 Table1 有两个字符串列,称为“ID”和“DATA”,Table2 有一个“UID”列和一个“FATDATA”列。 Table2 中的 id 都是唯一的,但在 Table1 中不是唯一的,并且假定 Table1 中的 ID 在 Table2 中显示为 UID。

**Table1**
ID DATA OTHERSTUFF
id1 ...
id1 ...
id2 ...
id2 ...
id3 ...

**Table2**
UID FATDATA MORESTUFF
id1 'adsf<data1>asdgas' ...
id2 'hrhdfh<data2>sdh' ...
id3 'adhfdh<data3>adf' ...

我需要做的是使用表 2 中的 FATDATA 填充表 1 的 DATA 列。具体来说,对于 Table1 中的每一行,我需要获取其 ID,在 Table2 中查找唯一对应的行,然后使用特定过程修剪其 FATDATA 值,并将结果存储在 Table1 中的行中。

最终结果应该是

**Table1**
ID DATA OTHERSTUFF
id1 'data1' ...
id1 'data1' ...
id2 'data2' ...
id2 'data2' ...
id3 'data3' ...

我可以像吴宇森那样解决question问题吗?在开始时提到,除了我自己的“TRIM(str)”函数而不是“AVG(intCol)”?再说一次,我对 SQL 还很陌生,所以我什至无法理解他的代码在做什么。

最佳答案

update Table1 set DATA = (select FATDATA from Table2 where UID = Table1.ID)
where exists (select FATDATA from Table2 where UID = Table1.ID)

关于mysql - 使用来自不同表的修改后的数据填充表。 (SQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54083663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com