数据库技术,mysql,oracle
select distinct name from table查询不重复记录的单个字段 只能取一个字段
select u_tel,max(u_name) u_name from mina2_distributor group by u_tel 按u_tel去重,并获取u_name字段值
select * from table where name in(select name from table group by name having count(name)=1)查询不重复(可设定重复数为任意次数)的所有记录 查询所有某字段不重复的记录
当表中存在唯一性的字段(如自增主键)时,例如唯一性id字段:
select * from table where id in ( select max (id) from table group by a,b,c...)
select * from table where fid in(Select min(fid) FROM table group by name)查询不重复记录的所有字段值
使用分析函数:
select t1.* from (
select t.*,row_number() over(partition by column_l order by column_aq asc) as rs_num from table t where xxxx order by xxxx)t1
where t1.rs_num=1; //对column_l去重,重复的数据按照column_aq排序取第一条。 over()里通过排序可以灵活的选择重复数据具体取哪一条,同时主sql的排序不受影响
语句结构:select t1.* from (select 正常要查询的字段,row_number() over(partition by 去重的字段 order by 去重的排序字段) as rs_num from 正常sql的from后面语句)t1 where t1.rs_num=1;
distinct多个字段会返回几个字段值都不重复的数据。
select distinct id,name from table 对于id和name都不一样的才去重;若id相同name不同,或者name相同id不同,都会返回,不会去重;
group by多个字段会筛选多个字段都不同的数据;
select id,name from table group by id,name 对于id和name都一样的去重只返回一条,id和name有其中一个不一样的不会去重;