分组前几个
根据某个字段进行分组获取前几个
select a.* from data a where 3 > (select count(*) from data where brand_id = a.brand_id
and total_num > a.total_num ) order by a.brand_id, a.total_num desc;
SELECT a.* FROM `t_user` a where 2> (select count(*) from t_user where `name`=a.`name`
and `password`>a.`password`) ORDER BY `name`
SELECT g.* FROM tt_task_progress g WHERE 1>(SELECT COUNT(*) FROM tt_task_progress WHERE task_id = g.`task_id` AND user_id = g.user_id AND `update_time`>g.`update_time`)
但是当数据大的时候性能很差
分组最大值
## 表单中每个分组都是取最大值
采用b来先获取到都是最大值,然后a表去联表
```sql
SELECT
a.*
FROM
crm_market_operate_record a
INNER JOIN (
SELECT
business_id,
max( create_time ) AS create_time
FROM
crm_market_operate_record
WHERE
business_type = 1
AND pool_entry_method IN ( 1, 2, 3 )
AND is_delete = 0
GROUP BY
business_id
) b ON a.business_id = b.business_id
AND a.create_time = b.create_time