質問編集履歴
2
誤字
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,8 +116,3 @@
|
|
116
116
|
|:--|:--:|:--:|:--:|--:|
|
117
117
|
|3|123|machine1|1|B001
|
118
118
|
|5|223|machine2|2|A003
|
119
|
-
|
120
|
-
|
121
|
-
|列1|列2|列3|1|
|
122
|
-
|:--|:--:|:--:|--:|
|
123
|
-
|
1
質問の仕方でアドバイスいただきましたので内容を修正しました。データをcreate tableで追加 SQLの修正 得たい結果の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
質問の仕方でアドバイスいただきましたので内容を修正しました。
|
1
2
|
3個のテーブルを結合したいのですがうまくいきません
|
2
3
|
Mysql8.0です。
|
3
4
|
|
@@ -12,55 +13,111 @@
|
|
12
13
|
Error: ER_NO_SUCH_TABLE: Table 'pa.latest2' doesn't exist
|
13
14
|
となってしまします。なぜそうなってしまうのかわからず教えて下さい
|
14
15
|
|
16
|
+
質問途中で②に対して①と③をjoinすればよかったと思いましたが
|
17
|
+
テーブルをまたいだ結合方法として知りたいのでご教授いただきたく
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
15
22
|
### 2テーブル成功
|
16
23
|
```SQL
|
17
24
|
SELECT
|
18
|
-
p
|
25
|
+
psi.id AS id,
|
19
|
-
p
|
26
|
+
psi.ieee_address AS ieee_address,
|
20
|
-
|
27
|
+
machine_m.machine_name AS name,
|
21
|
-
|
22
|
-
p
|
28
|
+
psi.red_information AS red
|
23
|
-
pa.qsi.wdt_monitoring_information AS wdt_monitoring_information,
|
24
|
-
pa.qsi.time_counter AS time_counter
|
25
29
|
FROM
|
26
|
-
(
|
30
|
+
(psi
|
27
31
|
JOIN (SELECT
|
28
|
-
p
|
32
|
+
psi.ieee_address AS ieee_address,
|
29
|
-
MAX(p
|
33
|
+
MAX(psi.id) AS ID
|
30
34
|
FROM
|
31
|
-
p
|
35
|
+
psi
|
32
|
-
GROUP BY p
|
36
|
+
GROUP BY psi.ieee_address) latest ON ((psi.ieee_address = latest.ieee_address)
|
33
|
-
AND (p
|
37
|
+
AND (psi.id = latest.ID))
|
34
|
-
JOIN
|
38
|
+
JOIN machine_m ON (latest.ieee_address = machine_m.ieee));
|
35
39
|
```
|
36
40
|
|
37
41
|
### 3テーブルを結合失敗
|
38
42
|
```SQL
|
39
43
|
SELECT
|
40
|
-
p
|
44
|
+
psi.id AS id,
|
41
|
-
p
|
45
|
+
psi.ieee_address AS ieee_address,
|
42
|
-
|
46
|
+
machine_m.machine_name AS name,
|
43
|
-
p
|
47
|
+
psi.red_information AS red,
|
44
|
-
pa.wsi.wdt_monitoring_information AS wdt_monitoring_information,
|
45
|
-
pa.wsi.time_counter AS time_counter,
|
46
|
-
|
48
|
+
send_program.toolno as toolno
|
47
49
|
FROM
|
48
|
-
((p
|
50
|
+
((psi
|
49
51
|
JOIN (SELECT
|
50
|
-
p
|
52
|
+
psi.ieee_address AS ieee_address,
|
51
|
-
MAX(p
|
53
|
+
MAX(psi.id) AS ID
|
52
54
|
FROM
|
53
|
-
p
|
55
|
+
psi
|
54
|
-
GROUP BY p
|
56
|
+
GROUP BY psi.ieee_address) latest ON ((psi.ieee_address = latest.ieee_address)
|
55
|
-
AND (p
|
57
|
+
AND (psi.id = latest.ID))
|
56
58
|
JOIN (select
|
57
|
-
|
59
|
+
send_program.machine AS machine,
|
58
|
-
MAX(
|
60
|
+
MAX(send_program.ID) AS ID2
|
59
61
|
FROM
|
60
|
-
|
62
|
+
send_program
|
61
|
-
GROUP BY
|
63
|
+
GROUP BY send_program.machine) latest2 ON ((send_program.machine = latest2.machine)
|
62
|
-
AND (
|
64
|
+
AND (send_program.id = latest2.ID2)))
|
63
65
|
|
64
|
-
join latest2 ON
|
66
|
+
join latest2 ON machine_m.machine_name = latest2.machine)
|
65
|
-
join
|
67
|
+
join machine_m ON (latest.ieee_address = machine_m.ieee);
|
66
68
|
```
|
69
|
+
データ例
|
70
|
+
```SQL
|
71
|
+
create table psi(
|
72
|
+
id INT NOT NULL PRIMARY KEY,
|
73
|
+
ieee_address varchar(50) null,
|
74
|
+
red_information smallint(5) null
|
75
|
+
);
|
76
|
+
|
77
|
+
INSERT INTO psi
|
78
|
+
(id,ieee_address,red_information)
|
79
|
+
VALUES
|
80
|
+
(1,123,1),
|
81
|
+
(2,123,0),
|
82
|
+
(3,123,1),
|
83
|
+
(4,223,0),
|
84
|
+
(5,223,2);
|
85
|
+
|
86
|
+
create table machine_m(
|
87
|
+
id INT NOT NULL PRIMARY KEY,
|
88
|
+
ieee varchar(50) null,
|
89
|
+
machine_name tinytext null
|
90
|
+
);
|
91
|
+
|
92
|
+
INSERT INTO machine_m
|
93
|
+
(id,ieee,machine_name)
|
94
|
+
VALUES
|
95
|
+
(1,123,"machine1"),
|
96
|
+
(2,223,"machine2");
|
97
|
+
|
98
|
+
create table send_program(
|
99
|
+
id INT NOT NULL PRIMARY KEY,
|
100
|
+
toolno tinytext null,
|
101
|
+
machine tinytext null
|
102
|
+
);
|
103
|
+
|
104
|
+
INSERT INTO send_program
|
105
|
+
(id,toolno,machine)
|
106
|
+
VALUES
|
107
|
+
(1,"A001","machine1"),
|
108
|
+
(2,"A002","machine2"),
|
109
|
+
(3,"B001","machine1"),
|
110
|
+
(4,"A003","machine2")
|
111
|
+
;
|
112
|
+
```
|
113
|
+
### 得たい結果
|
114
|
+
|
115
|
+
|ID|ieee_address|name|red|toolno|
|
116
|
+
|:--|:--:|:--:|:--:|--:|
|
117
|
+
|3|123|machine1|1|B001
|
118
|
+
|5|223|machine2|2|A003
|
119
|
+
|
120
|
+
|
121
|
+
|列1|列2|列3|1|
|
122
|
+
|:--|:--:|:--:|--:|
|
123
|
+
|