[英]Access 2013 - One to Many relationship: Show all values on “One” side and only select values on “Many” side
[英]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.