我有一个主表(t1)
id value group
------------------------------
5 22 1
6 55 1
7 18 2
8 11 2
缓存表(t2)
id value group
------------------------------
1 12 1
2 30 1
3 18 2
4 11 2
主表自动递增,因此每次保存数据时,表都会被清除,并且每次都会创建新的ID。
我需要用t1.id更新t2.id,以便它们匹配。
缓存表(t2)的必需结果:
id value group
------------------------------
5 12 1
6 30 1
7 18 2
8 11 2
Attempt1:
UPDATE t1, t2 SET t1.id=t2.id WHERE t1.id < t2.id ORDER BY id ASC
ATTEMPT2:
UPDATE t1, t2 SET t1.id = t2.id WHERE t1.id IS < MIN(t2.id) ORDER BY t1.id ASC
Attempt3:
UPDATE t1
INNER JOIN (
SELECT
MIN(t1.id) AS ID
FROM t1
GROUP BY ID) m ON t1.ID = m.ID
INNER JOIN t2 ON t1.ID = t2.ID
答案 0 :(得分:1)
我的尝试,试着让我知道:
Update cache t2
set t2.id = t2.id +
(select min(t1.id)-min(t2.id) from main t1, cache t2);
修改强>
如果你可以做两步查询,那就容易多了。
DECLARE diffValue INTEGER;
SELECT min(t1.id)-min(t2.id) into diffValue from main t1, cache t2;
UPDATE cache t2 set t2.id = t2.id + diffValue;
答案 1 :(得分:0)
您无法在更新语法中使用联接。
首先删除表格中的所有记录:
DELETE * FROM t2;
然后在其中写入新数据:
INSERT INTO t2
(t2.id, t2.value, t2.group)
SELECT t1.id, t1.value, t1.group FROM t1
这是你想要的吗?