首页 > 解决方案 > 为每个客户获取最大值

问题描述

我正在尝试设置一个 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)

但它不工作

标签: mysqlsql

解决方案


早上好。

如果我理解你,你可以获得最近修改的寄存器。正确的?如果是这样,请尝试:

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


推荐阅读