我将向您展示一些表的内容。
Bolek=> SELECT id, description from "TOMBInput";
id | description
----+-------------------
1 | Virtual Input 111
2 | Virtual Input 112
3 | Virtual Input 113
4 | Virtual Input 114
(4 rows)
Bolek=> SELECT id, setup_id FROM "TRBTOMBConnection";
id | setup_id
----+----------
1 | 1
2 | 1
3 | 1
4 | 1
(4 rows)
Bolek=> SELECT id, setname FROM "Setup";
id | setname
----+-------------
1 | SETUP_00001
(1 row)
Bolek=> SELECT id, setup_id FROM "Run";
id | setup_id
----+----------
1 | 1
(1 row)
我的查询[1]是
SELECT
"TOMBInput".id AS tombinput_id,
"TRBTOMBConnection".id AS trbtombconnection_id,
"Setup".id AS setup_id,
"Run".id AS run_id
FROM "TOMBInput"
INNER JOIN "TRBTOMBConnection" ON "TOMBInput".id = "TRBTOMBConnection".tombinput_id
FULL JOIN "Setup" ON "TRBTOMBConnection".id = "Setup".id
FULL JOIN "Run" ON "Setup".id = "Run".id AND "Run".id = 1;
结果表
tombinput_id | trbtombconnection_id | setup_id | run_id
--------------+----------------------+----------+--------
1 | 1 | 1 | 1
2 | 2 | |
3 | 3 | |
4 | 4 | |
(4 rows)
问题是 我希望有像
这样的表格 tombinput_id | trbtombconnection_id | setup_id | run_id
--------------+----------------------+----------+--------
1 | 1 | 1 | 1
2 | 2 | 1 | 1
3 | 3 | 1 | 1
4 | 4 | 1 | 1
(4 rows)
因为“TRBTOMBConnection”有4行,setup_id == 1 并且“运行”有setup_id == 1.
更重要的是,现在我改变最后一行(在我的查询[1]中)
FULL JOIN "Run" ON "Setup".id = "Run".id AND "Run".id = 2;
(在“运行”表中我们没有得到id == 2)查询的结果是
tombinput_id | trbtombconnection_id | setup_id | run_id
--------------+----------------------+----------+--------
1 | 1 | 1 |
2 | 2 | |
3 | 3 | |
4 | 4 | |
| | | 1
(5 rows)
没关系,因为我使用了FULL JOIN。
但是在这种情况下我运行查询[1]
我想有一个空的结果表,因为“运行”没有得到id == 2并且它没有任何意义来显示表,因为一切都是从Run开始的。
如何更改查询[1]?
答案 0 :(得分:2)
您的ID令人困惑:
SELECT
"TOMBInput".id AS tombinput_id,
"TRBTOMBConnection".id AS trbtombconnection_id,
"Setup".id AS setup_id,
"Run".id AS run_id
FROM "TOMBInput"
INNER JOIN "TRBTOMBConnection" ON "TOMBInput".id = "TRBTOMBConnection".tombinput_id
INNER JOIN "Setup" ON "TRBTOMBConnection".setup_id = "Setup".id
INNER JOIN "Run" ON "Setup".id = "Run".setup_id AND "Run".id = 1;
我认为没有理由在这里进行全外连接。