Double Join 以基于其他两个 table 从 table 获取数据
Double Join to get data from a table based on other two tables
我有三个 table 具有以下关键字段:
CONTRACTS
reference
package
EVENTS
reference
condition1
condition2
TRADES
reference
event_reference
基本上,我想做的是:
- 获取table
EVENTS
中满足两个条件(condition1
和condition2
)的所有reference
;
- 因此,获取 table
TRADES
的所有 reference
,其中 TRADES.event_reference = EVENTS.reference
- 最后,在
CONTRACTS.reference = TRADES.reference
处得到 CONTRACTS.package
(在第 2 点过滤数据之后)。
为了做到这一点,我尝试了 JOIN
语句:
SELECT CONTRACTS.package
FROM CONTRACTS
JOIN TRADES ON CONTRACTS.reference = TRADES.reference
JOIN EVENTS ON TRADES.event_reference = EVENTS.reference
WHERE EVENTS.condition1 = '1.511' AND EVENTS.condition2 IN (1,2)
但是,上面的(执行时没有错误)不会发出任何结果,我实际上希望看到一些结果。
因此,我明白我遵循的逻辑是错误的:有人可以帮忙吗?
编辑:这是数据的示例(黄色,我突出显示了查询中将触及的数据,如果它像我一样工作的话)记住它:
...这是预期结果:
1 (package of 4, related to 11 which satisfies condition 1 and 2)
2 (package of 6, related to 13 which satisfies condition 1 and 2)
4 (package of 10, related to 16 which satisfies condition 1 and 2)
这里是要复制粘贴的数据:
CONTRACTS
reference package
1 1
2 1
3 1
4 1
5 2
6 2
7 3
8 3
9 4
10 4
EVENTS
reference condition1 condition2
10 1.511 0
11 1.511 1
12 1.202 0
13 1.511 2
14 1.511 0
15 1.202 0
16 1.511 1
TRADES
reference event_reference
2 10
4 11
5 12
6 13
7 14
9 15
10 16
您的查询看起来没问题
SELECT CONTRACTS.package
FROM CONTRACTS
JOIN TRADES ON CONTRACTS.reference = TRADES.reference
JOIN EVENTS ON TRADES.event_reference = EVENTS.reference
WHERE EVENTS.condition1 = 'true' AND EVENTS.condition2 = 'true'
输出
| package |
|---------|
| 1 |
| 2 |
| 4 |
我有三个 table 具有以下关键字段:
CONTRACTS
reference
package
EVENTS
reference
condition1
condition2
TRADES
reference
event_reference
基本上,我想做的是:
- 获取table
EVENTS
中满足两个条件(condition1
和condition2
)的所有reference
; - 因此,获取 table
TRADES
的所有reference
,其中TRADES.event_reference = EVENTS.reference
- 最后,在
CONTRACTS.reference = TRADES.reference
处得到CONTRACTS.package
(在第 2 点过滤数据之后)。
为了做到这一点,我尝试了 JOIN
语句:
SELECT CONTRACTS.package
FROM CONTRACTS
JOIN TRADES ON CONTRACTS.reference = TRADES.reference
JOIN EVENTS ON TRADES.event_reference = EVENTS.reference
WHERE EVENTS.condition1 = '1.511' AND EVENTS.condition2 IN (1,2)
但是,上面的(执行时没有错误)不会发出任何结果,我实际上希望看到一些结果。 因此,我明白我遵循的逻辑是错误的:有人可以帮忙吗?
编辑:这是数据的示例(黄色,我突出显示了查询中将触及的数据,如果它像我一样工作的话)记住它:
...这是预期结果:
1 (package of 4, related to 11 which satisfies condition 1 and 2)
2 (package of 6, related to 13 which satisfies condition 1 and 2)
4 (package of 10, related to 16 which satisfies condition 1 and 2)
这里是要复制粘贴的数据:
CONTRACTS
reference package
1 1
2 1
3 1
4 1
5 2
6 2
7 3
8 3
9 4
10 4
EVENTS
reference condition1 condition2
10 1.511 0
11 1.511 1
12 1.202 0
13 1.511 2
14 1.511 0
15 1.202 0
16 1.511 1
TRADES
reference event_reference
2 10
4 11
5 12
6 13
7 14
9 15
10 16
您的查询看起来没问题
SELECT CONTRACTS.package
FROM CONTRACTS
JOIN TRADES ON CONTRACTS.reference = TRADES.reference
JOIN EVENTS ON TRADES.event_reference = EVENTS.reference
WHERE EVENTS.condition1 = 'true' AND EVENTS.condition2 = 'true'
输出
| package |
|---------|
| 1 |
| 2 |
| 4 |