设T_A与T_B是靠商品号prod_id关联, select * from (select * from T_A order by add_time desc limit 10) A /* 先找到 最新10条 */ join (select prod_id, max(order_time) max_order_time from T_B) B using (A.prod_id = T_B.prod_id) /* 找到相应产品的最大下单时间 */ join (select customer_id from T_B) B2 using (B.prod_id = B2.prod_id and B.max_order_time = B2.order_time) /* 连接取customer_id */
[征集]分组取最大N条记录方法征集,及散分....
select *
from (select * from T_A order by add_time desc limit 10) A /* 先找到 最新10条 */
join (select prod_id, max(order_time) max_order_time from T_B) B using (A.prod_id = T_B.prod_id) /* 找到相应产品的最大下单时间 */
join (select customer_id from T_B) B2 using (B.prod_id = B2.prod_id and B.max_order_time = B2.order_time) /* 连接取customer_id */