由于设计需要,需要查询不重复的记录值。例如有如下表结构和值tablefid name sex1 a 男2 b 男3 c 女4 d 女5 a 男6 b 男
方案一:distinctselect distinct name from table得到结果:nameabcd
那如果要同时name和sex字段都重复才被筛选,则使用以下语句:
select distinct name, sex from table
方案二:group by
select min(fid),name,sex from table group by name得到结果:fid name sex1 a 男2 b 男3 c 女4 d 女
如果要打开所有记录,不指定字段用(*),使用如下语句:
select * from table where fid in(Select min(fid) FROM table group by name)得到结果:
fid name sex1 a 男2 b 男3 c 女4 d 女
方案三:查询数据中所有某字段重复的记录select * from table where name in(select name from table group by name having count(name)=2) 得到如下结果:fid name sex1 a 男2 b 男5 a 男6 b 男以此类推:select * from table where name in(select name from table group by name having count(name)=1)