我有三个表格,其中包含以下关键字段:
CONTRACTS
reference
package
EVENTS
reference
condition1
condition2
TRADES
reference
event_reference
基本上,我想做的是:
reference
和EVENTS
)的表condition1
的所有condition2
; reference
所有TRADES
TRADES.event_reference = EVENTS.reference
CONTRACTS.package
CONTRACTS.reference = TRADES.reference
(在点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)
但是,上面(执行没有错误)不会发出任何结果,我实际上希望看到一些。 因此,我明白我所遵循的逻辑是错误的:有人可以帮忙吗?
编辑:这是数据外观的一个示例(黄色,我突出显示了查询中涉及的数据,如果它按照我的想法运行:< / p>
...这是预期的结果:
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
答案 0 :(得分:1)
您的查询看起来不错
<强> SQL Fiddle Demo 强>
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 |