我正在尝试设置一个SQL查询,以这种方式为每个用户获取具有update_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)
但是它不起作用
答案 0 :(得分:0)
使用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
)
答案 1 :(得分:0)
我认为您只需要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;
答案 2 :(得分:0)
早上好。
如果我了解您,则可以获取最新的修改过的寄存器。正确?如果是这样,请尝试:
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