0

我有 2 个表,我需要使用 table1 中的列更新 table2 中的列。table2.id2 是空的,我必须使用 table1.id 填充它。table1.code此外,您必须知道我在这些表(和)中有 2 列可以相互匹配table2.code。这是我的 SQL:

UPDATE table2 SET table2.id2 = table1.id WHERE table2.code = table1.code;

这个查询对吗?我收到了这个错误,但我确定它table1.code存在。

[Err] 1054 - 'where 子句' 中的未知列 'table1.code'

4

2 回答 2

1

假设您可以使用代码连接两个表

UPDATE T2
  JOIN T1 ON T1.CODE = T2.CODE
SET
  T2.ID2 = T1.ID
WHERE
  T2.ID2 = '';
于 2012-08-09T15:35:17.880 回答
0

那样不行。

拿着这个:

UPDATE table2 SET id2 = (SELECT id from table1 WHERE code = 'somecode') WHERE code = 'somecode';
于 2012-08-09T14:56:13.580 回答