(我的)SQL多深入左连接

时间:2012-07-04 05:25:06

标签: mysql sql join

有人可以告诉我如何将视图设为多个左连接吗?

A GAME has zero or multiple LINKs (OneToMany on GAME.ID = LINK.GAME_ID)
Each LINK has exactly one of multiple STOREs (OneToOne on LINK.STORE_ID = STORE.ID)
Each STORE has exactly one of multiple PLATFORMs (OneToOne on STORE.PLATFORM_ID = PLATFORM.ID)

我想将视图设为

PLATFORM_ID (FK)    GAME_ID (FK)
--------------------------------

我的意思是我希望按给定的GAME列出PLATFORM个,每个至少一个或多个路径LINK和{{ 1}}到STORE

谢谢。

3 个答案:

答案 0 :(得分:3)

select 
   l.game_id, p.platform_id
from
   links l,
   stores s,
   platforms p
where
   l.link_id = s.link_id
and
   p.platform_id = s.platform_id
group by 
   l.game_id, p.platform_id

答案 1 :(得分:2)

SELECT DISTINCT s.PLATFORM_ID, l.GAME_ID
FROM Games g 
LEFT JOIN Links l ON g.ID = l.GAME_ID
INNER JOIN Stores ON l.STORE_ID = s.STORE_ID
INNER JOIN PLATFORMS p ON s.PLATFORM_ID = p.ID

答案 2 :(得分:1)

你的SQL将是,

select GAME_ID 
from GAME,STORE,PLATFORM
where 
PLATFORM.ID = STORE.PLATFORM_ID
and STORE.ID = GAME.STORE_ID
and PLATFORM.ID = {some_ID}