Mysql 为每个客户端获取最大值

Mysql 为每个客户端获取最大值,mysql,sql,Mysql,Sql,我正在尝试设置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) 但它不起

我正在尝试设置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)
但它不起作用

使用corelates子查询

select c1.* from clients_records c1
where c1.updated_at=( select max(updated_at) from clients_records
                   c2 where c1.id_client=c2.id_client
                     )

我想您只需要
MAX()
函数:

select cr.id_client, max(csr.updated_at), from_unixtime(max(csr.updated_at))
from clients_records  cr join 
     clients_survey_records csr 
     on cr.id = csr.id 
group by id_client;
早上好

如果我理解你的意思,你可以得到最近修改过的注册表。对的如果是这样,请尝试:

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;
您可以通过更新的_轻松订购调查记录

一些链接:

请限定列名,以便清楚列的来源。