MySQL按条件分组,取出每组最新一条数据

SELECT
	* 
FROM
	(
SELECT
	a.sn,
	a.date_end,
	ifnull( b.renew_end, '' ) AS renew_end,
	d.msg_type,
	d.manager,
	c.realname,
	d.NAME 
FROM
	server_contract_tbl a
	LEFT JOIN server_renew_tbl b ON a.sn = b.sc_sn
	LEFT JOIN contract_tbl d ON a.contract_sn = d.sn
	LEFT JOIN staff_tbl c ON d.manager = c.sn 
WHERE
	a.is_del != 1 
ORDER BY
	b.renew_end DESC 
	) x 
GROUP BY
	x.sn 
ORDER BY
	x.renew_end DESC;

 

你可能感兴趣的:(sql)