mysql - 如何在mysql中的相应列的单行中获取多个行值
问题描述
我有一张这样的桌子
table | last_access | last_read | read_by | last_write | write_by | last_str_change | str_change_by
tab1 | 01-02-21 | 01-02-21 | user1 | null | null | null | null
tab1 | 02-02-21 | null | null | 02-02-21 | user2 | null | null
tab1 | 03-02-21 | null | null | null | null | 03-02-21 | user3
我需要这种格式
table last_access last_read read_by last_write write_by last_str_change str_change_by
tab1 03-02-21 01-02-21 user1 02-02-21 user2 03-02-21 user3
last_access 列应该具有来自 last_read、last_write、last_str_change 列的最大或最新日期,并且列 last_read、last_write、last_str_change 数据应该在一行中。
提前致谢。
解决方案
使用聚合:
select table, max(last_access), max(last_read), max(last_write), max(last_str_change)
from t
group by table;
推荐阅读
- python - Python Selenium - 无法单击按钮(iFrame 已解决!)
- javascript - 在 Android 中使用 Cordova 捕获实时麦克风音频
- html - 如何强制div水平而不是垂直溢出?
- apache-spark - parquet 文件大小,firehose 与 spark
- php - 尝试访问 Microsoft Graph 时“无法获取本地颁发者证书”
- javascript - 如何使用 Jest 测试 React 组件的 onClick 行?
- python - 类型错误:
() 缺少 1 个必需的位置参数:“项目” - sql - 如果 n-1、n-2.. 结果在 n 的 30 天内,则在分区中获得第 n 个结果
- excel - 尝试用 Excel 单元格内容替换书签时出现 MS Word 自动化错误
- jquery - 通过 JQuery 检测元素上的内联样式/CSS 更改