需求:获取每个live_id的前30天数据。 注!!!这个效率很低,不要线上跑,小心炸锅 方法1: SELECT a.* FROM user_live a LEFT JOIN user_live b ON a.live_id=b.live_id AND a.create_time<b.create_time GROUP BY a.live_id,a.watch_num,a.create_time HAVING COUNT(b.live_id)<29 ORDER BY a.live_id,a.create_time ASC
方法2: SELECT a.* FROM user_live a WHERE 30 > (SELECT COUNT(*) FROM user_live WHERE live_id = a.live_id AND create_time > a.create_time ) ORDER BY a.live_id,a.create_time