mysql 根据两个字段值查询时如何去除重复数据

表结构 & 数据

id date name code

1 2015-1-1 张三 1111
2 2015-2-1 李四 2222
3 2015-3-1 张三 1111
4 2015-4-1 王五 3333
5 2015-5-1 赵六 4444

根据字段name和code的值 如果两个值都相同的数据 就按date最大的保留

例如
id为1和3的 name和code都一样 就保留date最大即Id为3的这条数据

求大神指导sql怎么写

假设表名为test:

select * from (select *, concat(name,code) as __f from test order by date desc) __t group by __f;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-02
删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)
看看这里:http://blog.csdn.net/anya/article/details/6407280
第2个回答  2015-11-02
select distinct a.* from 表 a where a.date = ( select max(b.date) from 表 b where b.name = a.name and b.code = a.code )
自己试试
第3个回答  2017-04-28
这个语句是查询`Index`,sequence都相同的数据
select * from (select *, concat(`Index`,sequence) as __f from tbl_item order by sequence desc) __t group by __f having count(__f)>=2;
select * from tbl_item where sequence = 1465399887
第4个回答  2015-11-02
select distinct id,date ,name,code from 表名 where 条件
在select 后加上一个distinct ,注意 distinct后面的字段不能是重复项的 比如id就可以 name code 就不行了