繁体   English   中英

与 IN 子句的一对多关系未给出所有多边值

[英]One to Many Relationship with IN clause not giving all the many side values

我有以下一对多关系表。

| event_id (PK) | event_date | event_location |
|---------------|------------|----------------|
| 1             | 01/01/2018 | Miami          |                
| 2             | 02/04/2018 | Tampa          | 

| performer_id (PK) | event_id (FK) | genre |
|-------------------|---------------|-------|
| 1                 | 1             |  A    |
| 2                 | 1             |  B    |
| 3                 | 2             |  A    |

当我将 A 类型传递给 IN 子句时(我想要的 in(A) 在下面

| event_id (PK) | event_location |genre|
|---------------|----------------|-----|
| 1             | Miami          |A    |
| 1             | Miami          |B    |
| 2             | Tampa          |A    |

先感谢您。

首先,您需要根据流派获取 event_id 列表,然后根据连接表中的 event_id 列表获取 select。

Select * from 
(Select t1.event_id, t1.event_location, t2.genre
From Table_1 as t1
Inner Join table_2 as t2 on t1.event_id = t2.event_id) as output_table
Where event_id In (Select event_id From Table_2  where genre In ('A'))

公用表表达式应该更好。

With cte1 as (Select event_id From Table_2  where genre In ('A')),

cte2 as (Select t1.event_id, t1.event_location, t2.genre
From Table_1 as t1
Inner Join table_2 as t2 on t1.event_id = t2.event_id)

Select * from cte2 
Inner Join cte1 on cte1.event_id = cte2.event_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM