sql—是否可以在两个表上进行这种多连接?

kmpatx3s  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(337)

我有两个表,我有麻烦加入它给我想要的输出。
第一个表叫做future。这是我今后的会议。

Date         Name    Subject          Importance    Location
7/08/2020    David   Work             1             London
7/08/2020    George  Updates          2             New York
7/08/2020    Frank   New Appointments 5             London
7/08/2020    Steph   Policy           1             Paris

第二个表称为previous。这是我以前开过的会议。

Date         Name    Subject         Importance    Location     Time      Rating
1/08/2020    David   Work            3             London       23.50     4
2/10/2018    David   Emails          3             New York     18.20     3
1/08/2019    George  New Appointments5             London       55.10     2
3/04/2020    Steph   Dismissal       1             Paris        33.20     5

现在我需要的是通过名字引用我以前的表来查看我以前和这个人的会议,我想要上一个表中的所有数据。我还需要限制它只显示最多5个以前的会议与每个人。

Date         Name    Subject         Importance    Location     Time      Rating
7/08/2020    David   Work            1             London       -         -
1/08/2020    David   Work            3             London       23.50     4
2/10/2018    David   Emails          3             New York     18.20     3
7/08/2020    George  Updates         2             New York     -         -
1/08/2019    George  New Appointments5             London       55.10     2

name列需要是一个左连接,但是我需要对其他列进行常规连接。还不确定如何将名称结果限制为相同值的最多5个。提前谢谢你的帮助。

7gs2gvoe

7gs2gvoe1#

基本上,你想要 union all :

select m.*
from ((select Date, Name, Subject, Importance, Location, NULL as time, NULL as rating
       from future
      ) union all
      (select Date, Name, Subject, Importance, Location, time, rating
       from previous
      ) 
     ) m
group by name, date desc;

您可以对此结果应用其他条件。现在还不清楚你真正想要什么样的条件,但这只是一个开始。

相关问题