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

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

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

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

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

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

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

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

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

7gs2gvoe

7gs2gvoe1#

基本上,你想要 union all :

  1. select m.*
  2. from ((select Date, Name, Subject, Importance, Location, NULL as time, NULL as rating
  3. from future
  4. ) union all
  5. (select Date, Name, Subject, Importance, Location, time, rating
  6. from previous
  7. )
  8. ) m
  9. group by name, date desc;

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

相关问题