質問の仕方でアドバイスいただきましたので内容を修正しました。
3個のテーブルを結合したいのですがうまくいきません
Mysql8.0です。
テーブル
①wsi
②machine_m
③send_program
があり
①のIDを[group by]したものと②をjoinして取得するのはうまくいきましたが
さらに③のIDを[group by]しようとすると
Error: ER_NO_SUCH_TABLE: Table 'pa.latest2' doesn't exist
となってしまします。なぜそうなってしまうのかわからず教えて下さい
質問途中で②に対して①と③をjoinすればよかったと思いましたが
テーブルをまたいだ結合方法として知りたいのでご教授いただきたく
2テーブル成功
SQL
1SELECT 2 psi.id AS id, 3 psi.ieee_address AS ieee_address, 4 machine_m.machine_name AS name, 5 psi.red_information AS red 6 FROM 7 (psi 8 JOIN (SELECT 9 psi.ieee_address AS ieee_address, 10 MAX(psi.id) AS ID 11 FROM 12 psi 13 GROUP BY psi.ieee_address) latest ON ((psi.ieee_address = latest.ieee_address) 14 AND (psi.id = latest.ID)) 15 JOIN machine_m ON (latest.ieee_address = machine_m.ieee));
3テーブルを結合失敗
SQL
1SELECT 2 psi.id AS id, 3 psi.ieee_address AS ieee_address, 4 machine_m.machine_name AS name, 5 psi.red_information AS red, 6 send_program.toolno as toolno 7 FROM 8 ((psi 9 JOIN (SELECT 10 psi.ieee_address AS ieee_address, 11 MAX(psi.id) AS ID 12 FROM 13 psi 14 GROUP BY psi.ieee_address) latest ON ((psi.ieee_address = latest.ieee_address) 15 AND (psi.id = latest.ID)) 16 JOIN (select 17 send_program.machine AS machine, 18 MAX(send_program.ID) AS ID2 19 FROM 20 send_program 21 GROUP BY send_program.machine) latest2 ON ((send_program.machine = latest2.machine) 22 AND (send_program.id = latest2.ID2))) 23 24 join latest2 ON machine_m.machine_name = latest2.machine) 25 join machine_m ON (latest.ieee_address = machine_m.ieee);
データ例
SQL
1create table psi( 2 id INT NOT NULL PRIMARY KEY, 3 ieee_address varchar(50) null, 4 red_information smallint(5) null 5); 6 7INSERT INTO psi 8 (id,ieee_address,red_information) 9VALUES 10 (1,123,1), 11 (2,123,0), 12 (3,123,1), 13 (4,223,0), 14 (5,223,2); 15 16create table machine_m( 17 id INT NOT NULL PRIMARY KEY, 18 ieee varchar(50) null, 19 machine_name tinytext null 20); 21 22INSERT INTO machine_m 23 (id,ieee,machine_name) 24VALUES 25 (1,123,"machine1"), 26 (2,223,"machine2"); 27 28create table send_program( 29 id INT NOT NULL PRIMARY KEY, 30 toolno tinytext null, 31 machine tinytext null 32); 33 34INSERT INTO send_program 35 (id,toolno,machine) 36VALUES 37 (1,"A001","machine1"), 38 (2,"A002","machine2"), 39 (3,"B001","machine1"), 40 (4,"A003","machine2") 41 ;
得たい結果
ID | ieee_address | name | red | toolno |
---|---|---|---|---|
3 | 123 | machine1 | 1 | B001 |
5 | 223 | machine2 | 2 | A003 |
回答1件
あなたの回答
tips
プレビュー