mysql - 为每个客户获取最大值
问题描述
我正在尝试设置一个 sql 查询,以通过这种方式为每个用户获取具有最新值 updated_at 的行:
select cr.id_client,cr.updated_at,from_unixtime(cr.updated_at)
from clients_records as cr
join clients_survey_records as csr
on cr.id=csr.id
group by id_client
having updated_at = max(updated_at)
但它不工作
解决方案
早上好。
如果我理解你,你可以获得最近修改的寄存器。正确的?如果是这样,请尝试:
SELECT cr.id_client,
(
//Some query to concat and group data contained at clients_survey_records that You need
) AS last_survey_records
FROM clients_records as cr
INNER JOIN clients_survey_records as cs
WHERE cr.id_client = cs.fk_client_id
ORDER BY cr.id_client;
您可以通过 updated_at 轻松订购调查记录。
一些链接: https ://www.w3resource.com/mysql/string-functions/mysql-concat-function.php https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions- and-grouping-group_concat.php https://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php
推荐阅读
- algorithm - Big theta 介于 big o 和 big omega 之间,还是既是 big o 又是 big omega?
- javascript - 如何在关系中使用 findOrCreate - Adonis.js
- python - 如何使用 python sdk 访问 Azure 存储表中的最新条目?
- git - Git拉和合并?
- python - 遍历行然后列以构建 ColHeader:ColValue 字符串 python
- r - 将三个数据框的内容合并为一列
- python - 如何在Python中并行调用列表中每个对象的方法
- sql - 如何在函数中返回动态列数?
- jquery - jquery - 如何将基于 url 的 pdf 文件转换为 base64 并下载
- r - R:匹配向量之间的元素 - 如何优化代码