首页 > 解决方案 > 可以在 2 个表上进行多重连接吗?

问题描述

我有 2 个表,我无法加入它来给我想要的输出。

第一个表称为 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

第二个表称为上一个。这是我以前的会议。


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 个相同的值。提前感谢您的帮助。

标签: sqljoinreferencelimit

解决方案


基本上,你想要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;

您可以将其他条件应用于此结果。目前尚不清楚您真正想要的其他条件,但这是一个开始。


推荐阅读