回答編集履歴
4
推敲
test
CHANGED
@@ -99,5 +99,3 @@
|
|
99
99
|
left join camera ON base.observed= camera.recorded and base.id = camera.id
|
100
100
|
|
101
101
|
```
|
102
|
-
|
103
|
-
速度的にはhatenaさんの回答の記述の方が早いと思われます。
|
3
追記
test
CHANGED
@@ -65,3 +65,39 @@
|
|
65
65
|
left join mybook4 ON base.id = mybook4.id
|
66
66
|
|
67
67
|
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
質問が変更されたので追記
|
72
|
+
|
73
|
+
--
|
74
|
+
|
75
|
+
```SQL
|
76
|
+
|
77
|
+
SELECT base.id, meter1.observed, meter2.observed, camera.recorded
|
78
|
+
|
79
|
+
FROM (
|
80
|
+
|
81
|
+
select observed, id from (
|
82
|
+
|
83
|
+
select observed, id from meter1
|
84
|
+
|
85
|
+
union all select observed, id from meter2
|
86
|
+
|
87
|
+
union all select recorded, id from camera
|
88
|
+
|
89
|
+
) step1
|
90
|
+
|
91
|
+
group by observed, id
|
92
|
+
|
93
|
+
) base
|
94
|
+
|
95
|
+
left join meter1 ON base.observed= meter1.observed and base.id = meter1.id
|
96
|
+
|
97
|
+
left join meter2 ON base.observed= meter2.observed and base.id = meter2.id
|
98
|
+
|
99
|
+
left join camera ON base.observed= camera.recorded and base.id = camera.id
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
速度的にはhatenaさんの回答の記述の方が早いと思われます。
|
2
追記
test
CHANGED
@@ -27,3 +27,41 @@
|
|
27
27
|
left join mybook4 ON base.id = mybook4.id
|
28
28
|
|
29
29
|
```
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
以下は冗長になりますが、unionマージでない分高速であるかもしれません。
|
34
|
+
|
35
|
+
特にidに対してindexが設定されているようならさらに。
|
36
|
+
|
37
|
+
```SQL
|
38
|
+
|
39
|
+
SELECT base.id, mybook.name, mybook2.name, mybook3.name, mybook4.name
|
40
|
+
|
41
|
+
FROM (
|
42
|
+
|
43
|
+
select id from (
|
44
|
+
|
45
|
+
select id from mybook
|
46
|
+
|
47
|
+
union all select id from mybook2
|
48
|
+
|
49
|
+
union all select id from mybook3
|
50
|
+
|
51
|
+
union all select id from mybook4
|
52
|
+
|
53
|
+
) step1
|
54
|
+
|
55
|
+
group by id
|
56
|
+
|
57
|
+
) base
|
58
|
+
|
59
|
+
left join mybook ON base.id = mybook.id
|
60
|
+
|
61
|
+
left join mybook2 ON base.id = mybook2.id
|
62
|
+
|
63
|
+
left join mybook3 ON base.id = mybook3.id
|
64
|
+
|
65
|
+
left join mybook4 ON base.id = mybook4.id
|
66
|
+
|
67
|
+
```
|
1
推敲
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
idを集約するのにfull outerでは無理があります。
|
1
|
+
idを集約するのに、3つ以上のテーブルでfull outerで行うには無理があります。
|
2
2
|
|
3
3
|
一旦全てのIDのリストを取得し、それを元に結合します。
|
4
4
|
|