質問編集履歴
4
URL修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
下記にて「今のコード」を記入済みのため、動作を試せます。
|
4
4
|
|
5
|
-
[http://sqlfiddle.com/#!17/
|
5
|
+
[http://sqlfiddle.com/#!17/a6df3c3/1](http://sqlfiddle.com/#!17/a6df3c3/1)
|
6
6
|
|
7
7
|
|
8
8
|
|
3
piyopiyoとすべきところがpiyoになっていたので修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,37 +12,37 @@
|
|
12
12
|
|
13
13
|
「今のコード」のSELECT文の実行結果は、下記表のようになります。
|
14
14
|
|
15
|
-
| time | id | id | id | name | name | name |
|
15
|
+
| time | id | id | id | name | name | name |
|
16
16
|
|
17
|
-
|------------|----|----|----|----------|------|----------|
|
17
|
+
|------------|----|----|----|----------|----------|----------|
|
18
18
|
|
19
|
-
| 2020-08-11 | 1 | 1 | 1 | hoge | piyo | fuga |
|
19
|
+
| 2020-08-11 | 1 | 1 | 1 | hoge | piyo | fuga |
|
20
20
|
|
21
|
-
| 2020-08-11 | 1 | 2 | 1 | hoge | piyo | fuga |
|
21
|
+
| 2020-08-11 | 1 | 2 | 1 | hoge | piyopiyo | fuga |
|
22
22
|
|
23
|
-
| 2020-08-11 | 2 | 1 | 1 | hogehoge | piyo | fuga |
|
23
|
+
| 2020-08-11 | 2 | 1 | 1 | hogehoge | piyo | fuga |
|
24
24
|
|
25
|
-
| 2020-08-11 | 2 | 2 | 1 | hogehoge | piyo | fuga |
|
25
|
+
| 2020-08-11 | 2 | 2 | 1 | hogehoge | piyopiyo | fuga |
|
26
26
|
|
27
|
-
| 2020-08-11 | 1 | 1 | 2 | hoge | piyo | fugafuga |
|
27
|
+
| 2020-08-11 | 1 | 1 | 2 | hoge | piyo | fugafuga |
|
28
28
|
|
29
|
-
| 2020-08-11 | 1 | 2 | 2 | hoge | piyo | fugafuga |
|
29
|
+
| 2020-08-11 | 1 | 2 | 2 | hoge | piyopiyo | fugafuga |
|
30
30
|
|
31
|
-
| 2020-08-11 | 2 | 1 | 2 | hogehoge | piyo | fugafuga |
|
31
|
+
| 2020-08-11 | 2 | 1 | 2 | hogehoge | piyo | fugafuga |
|
32
32
|
|
33
|
-
| 2020-08-11 | 2 | 2 | 2 | hogehoge | piyo | fugafuga |
|
33
|
+
| 2020-08-11 | 2 | 2 | 2 | hogehoge | piyopiyo | fugafuga |
|
34
34
|
|
35
35
|
|
36
36
|
|
37
37
|
しかし、今得たいと思っているのは、下記です。
|
38
38
|
|
39
|
-
| time | id | id | id | name | name | name |
|
39
|
+
| time | id | id | id | name | name | name |
|
40
40
|
|
41
|
-
|------------|----|----|----|----------|------|----------|
|
41
|
+
|------------|----|----|----|----------|----------|----------|
|
42
42
|
|
43
|
-
| 2020-08-11 | 1 | 1 | 1 | hoge | piyo | fuga |
|
43
|
+
| 2020-08-11 | 1 | 1 | 1 | hoge | piyo | fuga |
|
44
44
|
|
45
|
-
| 2020-08-11 | 2 | 2 | 2 | hogehoge | piyo | fugafuga |
|
45
|
+
| 2020-08-11 | 2 | 2 | 2 | hogehoge | piyopiyo | fugafuga |
|
46
46
|
|
47
47
|
|
48
48
|
|
@@ -98,13 +98,11 @@
|
|
98
98
|
|
99
99
|
insert into meter2 values('2020-08-11','1', 'piyo');
|
100
100
|
|
101
|
-
insert into meter2 values('2020-08-11','2', 'piyo');
|
101
|
+
insert into meter2 values('2020-08-11','2', 'piyopiyo');
|
102
102
|
|
103
103
|
insert into camera values('2020-08-11','1', 'fuga');
|
104
104
|
|
105
105
|
insert into camera values('2020-08-11','2', 'fugafuga');
|
106
|
-
|
107
|
-
|
108
106
|
|
109
107
|
|
110
108
|
|
2
複数の条件を同時に満たすデータを取得したいのが本当にやりたかったことだったため、質問のコードを大幅に改定。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
SQL文で、
|
1
|
+
SQL文で、複数のテーブルから複数の条件に合うデータをSELECTしたい
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
下記にて「今のコード」を記入済みのため、動作を試せます。
|
4
4
|
|
5
|
-
[http://sqlfiddle.com/#!17/b
|
5
|
+
[http://sqlfiddle.com/#!17/1c36b](http://sqlfiddle.com/#!17/1c36b)
|
6
6
|
|
7
7
|
|
8
8
|
|
@@ -12,63 +12,43 @@
|
|
12
12
|
|
13
13
|
「今のコード」のSELECT文の実行結果は、下記表のようになります。
|
14
14
|
|
15
|
-
| id |
|
15
|
+
| time | id | id | id | name | name | name |
|
16
16
|
|
17
|
-
|----
|
17
|
+
|------------|----|----|----|----------|------|----------|
|
18
18
|
|
19
|
-
| 1 |
|
19
|
+
| 2020-08-11 | 1 | 1 | 1 | hoge | piyo | fuga |
|
20
20
|
|
21
|
-
| 2 |
|
21
|
+
| 2020-08-11 | 1 | 2 | 1 | hoge | piyo | fuga |
|
22
22
|
|
23
|
-
| 2 |
|
23
|
+
| 2020-08-11 | 2 | 1 | 1 | hogehoge | piyo | fuga |
|
24
|
+
|
25
|
+
| 2020-08-11 | 2 | 2 | 1 | hogehoge | piyo | fuga |
|
26
|
+
|
27
|
+
| 2020-08-11 | 1 | 1 | 2 | hoge | piyo | fugafuga |
|
28
|
+
|
29
|
+
| 2020-08-11 | 1 | 2 | 2 | hoge | piyo | fugafuga |
|
30
|
+
|
31
|
+
| 2020-08-11 | 2 | 1 | 2 | hogehoge | piyo | fugafuga |
|
32
|
+
|
33
|
+
| 2020-08-11 | 2 | 2 | 2 | hogehoge | piyo | fugafuga |
|
24
34
|
|
25
35
|
|
26
36
|
|
27
37
|
しかし、今得たいと思っているのは、下記です。
|
28
38
|
|
29
|
-
| id |
|
39
|
+
| time | id | id | id | name | name | name |
|
30
40
|
|
31
|
-
|----
|
41
|
+
|------------|----|----|----|----------|------|----------|
|
32
42
|
|
33
|
-
| 1 |
|
43
|
+
| 2020-08-11 | 1 | 1 | 1 | hoge | piyo | fuga |
|
34
44
|
|
35
|
-
| 2 |
|
45
|
+
| 2020-08-11 | 2 | 2 | 2 | hogehoge | piyo | fugafuga |
|
36
46
|
|
37
47
|
|
38
48
|
|
39
|
-
つまり
|
49
|
+
つまり、時間とidが同じもののみを結合させて取得したいのですが、
|
40
50
|
|
41
51
|
どのようにしたらよいでしょうか。
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
### 試したこと
|
46
|
-
|
47
|
-
下記のように書いてみましたがシンタックスエラーになりました。
|
48
|
-
|
49
|
-
```PostgreSQL
|
50
|
-
|
51
|
-
FROM
|
52
|
-
|
53
|
-
(mybook
|
54
|
-
|
55
|
-
FULL OUTER JOIN mybook2 ON mybook.id = mybook2.id
|
56
|
-
|
57
|
-
FULL OUTER JOIN mybook3 ON mybook.id = mybook3.id
|
58
|
-
|
59
|
-
FULL OUTER JOIN mybook4 ON mybook.id = mybook4.id)
|
60
|
-
|
61
|
-
OR
|
62
|
-
|
63
|
-
(mybook2
|
64
|
-
|
65
|
-
FULL OUTER JOIN mybook ON mybook2.id = mybook.id
|
66
|
-
|
67
|
-
FULL OUTER JOIN mybook3 ON mybook2.id = mybook3.id
|
68
|
-
|
69
|
-
FULL OUTER JOIN mybook4 ON mybook2.id = mybook4.id)
|
70
|
-
|
71
|
-
```
|
72
52
|
|
73
53
|
|
74
54
|
|
@@ -78,17 +58,21 @@
|
|
78
58
|
|
79
59
|
```PostgreSQL
|
80
60
|
|
81
|
-
create table m
|
61
|
+
create table meter1 (
|
82
62
|
|
63
|
+
observed DATE,
|
64
|
+
|
83
|
-
id integer,
|
65
|
+
id integer,
|
84
66
|
|
85
67
|
name varchar(10)
|
86
68
|
|
87
69
|
);
|
88
70
|
|
89
|
-
create table m
|
71
|
+
create table meter2 (
|
90
72
|
|
73
|
+
observed DATE,
|
74
|
+
|
91
|
-
id integer,
|
75
|
+
id integer,
|
92
76
|
|
93
77
|
name varchar(10)
|
94
78
|
|
@@ -96,9 +80,11 @@
|
|
96
80
|
|
97
81
|
|
98
82
|
|
99
|
-
create table m
|
83
|
+
create table camera (
|
100
84
|
|
85
|
+
recorded DATE,
|
86
|
+
|
101
|
-
id integer,
|
87
|
+
id integer,
|
102
88
|
|
103
89
|
name varchar(10)
|
104
90
|
|
@@ -106,60 +92,44 @@
|
|
106
92
|
|
107
93
|
|
108
94
|
|
109
|
-
|
95
|
+
insert into meter1 values('2020-08-11','1', 'hoge');
|
110
96
|
|
111
|
-
|
97
|
+
insert into meter1 values('2020-08-11','2', 'hogehoge');
|
112
98
|
|
113
|
-
|
99
|
+
insert into meter2 values('2020-08-11','1', 'piyo');
|
114
100
|
|
115
|
-
);
|
101
|
+
insert into meter2 values('2020-08-11','2', 'piyo');
|
102
|
+
|
103
|
+
insert into camera values('2020-08-11','1', 'fuga');
|
104
|
+
|
105
|
+
insert into camera values('2020-08-11','2', 'fugafuga');
|
116
106
|
|
117
107
|
|
118
108
|
|
119
|
-
insert into mybook values('1', 'hoge');
|
120
109
|
|
121
|
-
insert into mybook2 values('2', 'hogehoge');
|
122
|
-
|
123
|
-
insert into mybook3 values('2', 'piyo');
|
124
|
-
|
125
|
-
insert into mybook4 values('1', 'piyoyo');
|
126
|
-
|
127
|
-
```
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
```PostgreSQL
|
132
110
|
|
133
111
|
SELECT
|
134
112
|
|
135
113
|
case
|
136
114
|
|
137
|
-
when m
|
115
|
+
when meter1.observed is not null then meter1.observed
|
138
116
|
|
139
|
-
when m
|
117
|
+
when meter2.observed is not null then meter2.observed
|
140
118
|
|
141
|
-
when m
|
119
|
+
when camera.recorded is not null then camera.recorded
|
142
120
|
|
143
|
-
|
121
|
+
end as "time",
|
144
122
|
|
145
|
-
e
|
123
|
+
meter1.id, meter2.id, camera.id,
|
146
124
|
|
147
|
-
mybook.name,
|
148
|
-
|
149
|
-
m
|
125
|
+
meter1.name, meter2.name, camera.name
|
150
|
-
|
151
|
-
mybook3.name,
|
152
|
-
|
153
|
-
mybook4.name
|
154
126
|
|
155
127
|
FROM
|
156
128
|
|
157
|
-
m
|
129
|
+
meter1
|
158
130
|
|
159
|
-
FULL OUTER JOIN m
|
131
|
+
FULL OUTER JOIN meter2 ON (meter1.observed = meter2.observed)
|
160
132
|
|
161
|
-
FULL OUTER JOIN m
|
133
|
+
FULL OUTER JOIN camera ON (meter1.observed = camera.recorded)
|
162
|
-
|
163
|
-
FULL OUTER JOIN mybook4 ON mybook.id = mybook4.id
|
164
134
|
|
165
135
|
```
|
1
URLをリンク形式に修正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
-
オンライン実行環境
|
1
|
+
### オンライン実行環境
|
2
2
|
|
3
|
+
下記にて「今のコード」を記入済みのため、動作を試せます。
|
4
|
+
|
3
|
-
http://sqlfiddle.com/#!17/b5824/2
|
5
|
+
[http://sqlfiddle.com/#!17/b5824/2](http://sqlfiddle.com/#!17/b5824/2)
|
4
6
|
|
5
7
|
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
### 現状とやりたいこと
|
6
12
|
|
7
13
|
「今のコード」のSELECT文の実行結果は、下記表のようになります。
|
8
14
|
|