回答編集履歴
1
調整
test
CHANGED
@@ -5,3 +5,63 @@
|
|
5
5
|
timestamp(datetime)にすればcopyAのカラムXの最大値より大きなカラムXをもつAの
|
6
6
|
|
7
7
|
データを抽出すればよいでしょう
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
# sample
|
12
|
+
|
13
|
+
```SQL
|
14
|
+
|
15
|
+
create table tbl_a1(c1 varchar(20) not null primary key,c2 varchar(20),cx datetime default current_timestamp on update current_timestamp);
|
16
|
+
|
17
|
+
create table tbl_a2(c1 varchar(20) not null primary key,c2 varchar(20),cx datetime);
|
18
|
+
|
19
|
+
insert into tbl_a1 values('x001','y001','2018-11-01 00:00'),('x002','y002','2018-11-01 00:00'),('x003','y003','2018-11-01 00:00');
|
20
|
+
|
21
|
+
insert into tbl_a2 values('x001','y001','2018-11-01 00:00'),('x002','y002','2018-11-01 00:00'),('x003','y003','2018-11-01 00:00');
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
とりあえず同期されている状態にします。
|
26
|
+
|
27
|
+
ここからa1のデータをいじるとして
|
28
|
+
|
29
|
+
```SQL
|
30
|
+
|
31
|
+
update tbl_a1 set c2='y002x' WHERE c1 = 'x002';
|
32
|
+
|
33
|
+
delete from tbl_a1 where c1=('x003');
|
34
|
+
|
35
|
+
insert into tbl_a1(c1,c2) values('x004','y004');
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
x002は更新されてcxが現在の日時に、x004のデータは新規データです
|
40
|
+
|
41
|
+
x003は削除なのでa1にデータが残りません
|
42
|
+
|
43
|
+
このデータをa1→a2にするには
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```SQL
|
48
|
+
|
49
|
+
insert into tbl_a2
|
50
|
+
|
51
|
+
select * from tbl_a1 where cx>(select max(cx) from tbl_a2)
|
52
|
+
|
53
|
+
on duplicate key update c2=values(c2),cx=values(cx)
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
とします。
|
58
|
+
|
59
|
+
削除分まで同期する場合は
|
60
|
+
|
61
|
+
```SQL
|
62
|
+
|
63
|
+
delete from tbl_a2 where not exists(select 1 from tbl_a1 where tbl_a2.c1=c1)
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
も実行しておいてください
|