批量查用户的数据,每个用户都是对应最新的一条记录

1.单独需要这样查,循环查的话效率很慢。


select profit_rate_w as w_r,profit_rate_t as t_r ,profit_rate_m as m_r,profit_rate_d as d_r from replica_profit where u_id=36273 ORDER BY id desc limit 1;

select profit_rate_w as w_r,profit_rate_t as t_r ,profit_rate_m as m_r,profit_rate_d as d_r from replica_profit where u_id=66172 ORDER BY id desc limit 1;

select profit_rate_w as w_r,profit_rate_t as t_r ,profit_rate_m as m_r,profit_rate_d as d_r from replica_profit where u_id=72783 ORDER BY id desc limit 1;

select profit_rate_w as w_r,profit_rate_t as t_r ,profit_rate_m as m_r,profit_rate_d as d_r from replica_profit where u_id=178782 ORDER BY id desc limit 1;

select profit_rate_w as w_r,profit_rate_t as t_r ,profit_rate_m as m_r,profit_rate_d as d_r from replica_profit where u_id=189641 ORDER BY id desc limit 1;

 

2.可以改为批量查。

select profit_rate_w as w_r,profit_rate_t as t_r ,profit_rate_m as m_r,profit_rate_d as d_r,replica_profit.u_id
from replica_profit inner join
(select max(id) as id,u_id from replica_profit
where replica_profit.u_id in (36273,66172,72783,178782,189641)
group by u_id ) r
on (replica_profit.u_id=r.u_id and replica_profit.id=r.id);

posted @ 2021-08-31 16:49  17601621550  阅读(251)  评论(0编辑  收藏  举报