在SQL中加入三个表

时间:2011-11-08 21:59:42

标签: sql join

如果我想知道哪个“Mann”属于哪个“Frau”,如何加入SQL?

结果应如下所示:

FrauX "id" | MannX "id"
FrauY "id" | MannY "id" 

http://i.stack.imgur.com/Cv8NM.png

2 个答案:

答案 0 :(得分:3)

下面是联接,您可以选择查询所需的列

SELECT m.idMann
   , m.Name AS MannName
   , f.idFrau 
   , f.Name AS FrauName
FROM Mann AS m
INNER JOIN Frau_has_Mann AS fm ON m.idMann = fm.Mann_idMann
INNER JOIN Frau AS f ON fm.Frau_idFrau = f.idFrau

看起来混叠可能会造成麻烦,您可以尝试:

SELECT Mann.idMann
   , Mann.Name AS MannName
   , Frau.idFrau 
   , Frau.Name AS FrauName
FROM Mann
INNER JOIN Frau_has_Mann ON Mann.idMann = Frau_has_Mann.Mann_idMann
INNER JOIN Frau ON Frau_has_Mann.Frau_idFrau = Frau.idFrau

基本上删除了整个列名的别名。

答案 1 :(得分:0)

Select F.idFrau,M.idMann FROM Frau AS F INNER JOIN Frau_has_Mann AS FHM ON FHM.Frau_idFrau = F.idFrau INNER JOIN Mann AS M ON M.idMann = FHM.Mann_idMann

希望这就是你要找的东西。