使用distinct消除重复记录的同时又能选取多个字段值

需求是:我要消除name字段值重复的记录,同时又要得到id字段的值,其中id是自增字段。 
select distinct name from t1 能消除重复记录,但只能取一个字段,现在要同时取id,name这2个字段的值。 
select distinct id,name from t1 可以取多个字段,但只能消除这2个字段值全部相同的记录 
最后解决方法: 

SELECT id,name FROM t1 WHERE id IN(SELECT MAX(id) FROM t1 GROUP BY name) order by id desc

 

select distinct parameter_power_max, parameter_id from charge_parameter;

select * from charge_parameter
where parameter_id in (select max(parameter_id) from charge_parameter group by parameter_power_max) order by parameter_id;

 

posted @ 2019-03-09 15:52  陆伟  阅读(1899)  评论(0编辑  收藏  举报