mysql - 将最新值加入表
问题描述
我正在使用mysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using EditLine wrapper
.
我有一张叫桌子companies
和一张桌子fundamentals
。
我想将 的最新值加入fundamentals
表companies
中,而最新的是created_at
in fundamentals
。
请在下面找到我在 db-fiddle 上的最小示例:
我当前的查询如下所示:
select
*
from
companies c
left outer join fundamentals f on
f.companies_id = c.id
order by
f.created_at DESC;
感谢您的回复!
解决方案
您可以使用窗口函数:
select *
from companies c left outer join
(select f.*,
row_number() over (partition by f.companies_id order by f.created_at desc) as seqnum
from fundamentals f
) f
on f.companies_id = c.id and f.seqnum = 1
order by f.created_at DESC;
推荐阅读
- android - 我有办法从服务中获取图像资源吗?
- ios - 从 iOS 的 Xcode 目标中排除 Flutter 包
- reactjs - 图片未加载到页面上,但链接在浏览器中的检查元素上工作
- go - 我想知道为什么这两种方式的结果不同
- node.js - SDK中插入方法写入BigQuery表的限制是什么
- excel - 将满足条件的所有行复制到另一个工作表中的最后一个可用行
- random - Ansible 分配随机数并增加重试次数
- wpf - 如何用曲线环绕我的 WPF Datagrid 标题?
- javascript - 有没有更好的方法来合并 Javascript 中的对象数组?
- sql-server - 为了显示(描述)表的结构,我正在使用 DESC 命令,但出现错误,显示“关键字'DESC'附近的语法不正确”。