sql - 可以在 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 个相同的值。提前感谢您的帮助。
解决方案
基本上,你想要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;
您可以将其他条件应用于此结果。目前尚不清楚您真正想要的其他条件,但这是一个开始。
推荐阅读
- javascript - Vue - 基于开发或生产模式导入组件
- python - 使用元组在列上应用多个过滤器
- javascript - react-draft-wysiwyg 编辑器文本未显示某些样式
- elasticsearch - 用于嵌套查询的 ElasticSearch 相邻词
- javascript - 撤消 .removeAtribute 函数
- mongodb - 只返回一个对象数组中的一个对象
- linux - 即使在 Amazon Linux 容器中的所需位置有 google-chrome 可执行文件,也缺少 google-chrome
- java - 将 findByNamedQueryAndNamedParam(Srring-ORM 4) 升级到 getNamedQuery(Srping-ORM 5)
- object-detection - 如何使用 swin 变压器 (mmdetection) 分析结果?
- django - 如何创建不同的标签,这是 django wagtail 中的博客所独有的