我现在有一个位置表我必须为每个客户保留100条记录,并且必须将其余的记录移到另一台服务器上。
使用此查询我们可以获取客户列表
SELECT user_id FROM user_location_history WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30) GROUP BY user_id HAVING COUNT(1) < 100
现在假设我们有一个客户列表
我正在尝试编写一个查询来获取每个客户的所有记录,大于100。
请建议......
答案 0 :(得分:0)
这将获得每位客户的最新100条记录
SELECT user_id, substring_index(group_concat(sample_id ORDER BY sample_id DESC SEPARATOR ','), ',', 100) limit100, FROM user_location_history GROUP BY user_id ;
答案 1 :(得分:0)
尝试此查询,它会为每条记录生成一个行号: -
set @num := 0, @group := '';
select *
from
(
select user_id,
@num := if(@group = `user_id`, @num + 1, 1) as row_number,
@group := `user_id` as dummy
from user_location_history
WHERE (TIMESTAMPDIFF(DAY,FROM_UNIXTIME(location_date/1000),SYSDATE()) < 30)
) as x
where x.row_number <= 100;