mysql GROUP BY 取分组中最小(OR 最大)的一条记录

代码

select d.* FROM
sem_database_export as d
LEFT JOIN 
(
SELECT a.id,a.keyname,a.shebei,a.adverts_lei,a.adverts_pinpai,
MAX(a.page) as page,MAX(a.state) as state
from sem_database_export as a
GROUP BY a.keyname,a.shebei,a.adverts_lei,a.adverts_pinpai
) as t1 on d.keyname=t1.keyname and d.shebei=t1.shebei and d.adverts_lei = t1.adverts_lei 
and d.adverts_pinpai = t1.adverts_pinpai and d.page = t1.page and d.state = t1.state
WHERE t1.id is not NULL

未分组之前

mysql GROUP BY 取分组中最小(OR 最大)的一条记录_第1张图片

分组之后

mysql GROUP BY 取分组中最小(OR 最大)的一条记录_第2张图片

你可能感兴趣的:(mysql)