如何加入两个表和两个或更多列(Ms Access)?

时间:2015-12-19 09:56:59

标签: sql ms-access join

以下是我想加入查询的表格。

表1名称​​ Hasta

ID  -----   AdiSoyadi
1   -----   Mesut AKTAS
2   -----   Orhan Dogan
3   -----   Ali Veli
...

表2名称​​ islemler

Hasta_ID --------  islem_Tarihi------ Aciklama
1----------------- 12.12.2015---------Explain 1>
1------------------16.12.2015 --------Explain 2
2------------------20.12.2015---------Explain 3
2------------------19.12.2015---------Explain 4
3------------------18.12.2015---------Explain 5
3------------------01.01.2016---------Explain 6
3------------------02.01.2016---------Explain 7 
......

我想要这个结果:

islem_Tarihi------Hasta_ID----------AdiSoyadi-------Aciklama
02.01.2016--------3-----------------Ali Veli--------Explain 7
20.15.2015--------2-----------------Orhan Dogan-----Explain 3
16.12.2015--------1-----------------Mesut AKTAS-----Explain 2

这是我的SQL查询,它没有按预期工作:

SELECT t1.islem_Tarihi,t2.AdiSoyadi,t1.Aciklama
FROM islemler t1
inner join Hasta t2
    on t2.ID=t1.Hasta_ID
inner join (SELECT MAX(islem_Tarihi) AS Mak,AdiSoyadi
                  From islemler,Hasta
                  Where Hasta.ID=islemler.Hasta_ID
                  GROUP BY AdiSoyadi) t3
ON t2.AdiSoyadi=t3.AdiSoyadi And t1.islem_Tarihi=t3.Mak

如何对上述结果制定我的查询?

1 个答案:

答案 0 :(得分:0)

您似乎想要来自hasta的最新行。这是一种方法:

select i.islem_Tarihi, h.AdiSoyadi, h.Aciklama
from hasta as h inner join
     islemler as i
     on h.id = i.hasta_id
where i.islem_Tarihi = (select max(i2.islem_Tarihi
                        from islemler as i2
                        where i2.id = i.id
                       )
order by h.id desc;