日期:2014-05-18  浏览次数:20477 次

去除重复数据
create table tbqc
(
id int,
name varchar(30),
sex varchar(30),
age int 
)
insert tbqc
select 1,'a','nv',20 union all
select 1,'a','nv',20 union all
select 2,'b','nan',20 union all
select 2,'b','nan',20 union all
select 3,'c','nan',20 union all
select 4,'d','nv',20 union

------解决方案--------------------
SQL code

create table tbqc
(
id int,
name varchar(30),
sex varchar(30),
age int 
)
insert tbqc
select 1,'a','nv',20 union all
select 1,'a','nv',20 union all
select 2,'b','nan',20 union all
select 2,'b','nan',20 union all
select 3,'c','nan',20 union all
select 4,'d','nv',20

select distinct * from tbqc

/*
id          name                           sex                            age
----------- ------------------------------ ------------------------------ -----------
1           a                              nv                             20
2           b                              nan                            20
3           c                              nan                            20
4           d                              nv                             20
*/

------解决方案--------------------
SQL code

select distinct * from tbqc


/*
id          name                           sex                            age
----------- ------------------------------ ------------------------------ -----------
1           a                              nv                             20
2           b                              nan                            20
3           c                              nan                            20
4           d                              nv                             20

(4 行受影响)
*/

------解决方案--------------------
数据表设计的时候是应该有主键的,有主键的情况下是不可能出现完全相同的数据的。
------解决方案--------------------
[select distinct * from tbqc=SQL][/code]
------解决方案--------------------
对于表中两行记录完全一样的情况,可以用下面语句获取到去掉重复数据后的记录:
select distinct * from 表名