sql中删除一个表中的重复记录可以采用如下步骤:
1、把a_dist表的记录用distinct去重,结果放到临时表中。
select distinct * into #temp from a_dist;
2、把a_dist表的记录全部删除。
delete from a_dist;
3、把临时表中的数据信息导进到a_dist表中,并删除临时表。
insert into a_distselect * from #temp;
drop table #temp;
(1)sql查询时去掉重复组扩展阅读:
SQL (结构化查询语言)是用于执行查询的语法。在数据库上执行的大部分工作都由 SQL 语句完成。SQL 语言包含用于更新、插入和删除记录的语法。
增删改查指令构成了 SQL 的 DML 部分:
SELECT- 从数据库表中获取数据
UPDATE- 更新数据库表中的数据
DELETE- 从数据库表中删除数据
INSERT INTO- 向数据库表中插入数据
② SQL查询中如何剔除重复
1、存在部分字段相同的纪录
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group
代码:select
*
from
table
where
id
in
(select
max(id)
from
table
group
by
[去除重复的字段名列表,....])
2、存在两条完全相同的记录
这是最简单的一种情况,用关键字distinct就可以去掉
代码:select
distinct
*
from
table(表名)
where
(条件)
3、没有唯一键ID
这种较为复杂
代码:
select
identity(int1,1)
as
id,*
into
newtable(临时表)
from
table(原表)
select
*
from
newtable
where
id
in
(select
max(id)
from
newtable
group
by
[去除重复的字段名列表,....])
drop
table
newtable
(2)sql查询时去掉重复组扩展阅读:
SQL查询语句
1、查询全部的重复信息
select
*
from
people
where
id
not
in
(
select
min(id)
from
people
group
by
name,sex
HAVING
COUNT(*)
<
2)
2、查询多余的重复信息
select
*
from
people
where
id
not
in
(
select
MIN(id)
from
people
group
by
name,sex)
③ mysql查询去掉重复数据
可以利用distinct关键字对需要处理的字段进行去重
④ SQL语句查询 如何删除重复多余的数据
这个是SQL中distinct的典型用法:
1)从字面意思就可以了解到:
distinct
[dis'tiŋkt]
adj.
明显的;独特的;清楚的;有区别的
2)在SQL中用distinct来消除重复出现的字段值。
使得每个字段值只出现一次。
具体用法如下:
select
distinct
字段名
from
表;
distinct
字段名
意思就是只显示一次该字段名
一般情况下和order
by
结合使用,这样可以提高效率。
所以这个问题的答案是:select
distinct
1,2,3,4
from
表;
1,2,3,4分别代表第一,二,三,四列的字段名,我猜测可能第一列就是每个人的ID,
这样你把重复的ID过滤留下一个,估计就是你想要的结果了。
希望我的回答能让您满意。
⑤ 在sql语言中去掉重复值的命令是
distinct。
SQLserver中很明显的去重复的语句是distinct。selectdistinct是去除重复的记录行,count(distinctColumn),消除重复值。还有一些不明显的具有去重功能的词,例如union,会去除重复的记录行或值。
⑥ SQL查询中如何剔除重复
1,存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉
example: select distinct * from table(表名) where (条件)
2,存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
example:
select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])
3,没有唯一键ID
example:
select identity(int1,1) as id,* into newtable(临时表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
drop table newtable
(6)sql查询时去掉重复组扩展阅读
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
⑦ sql 查询时如何去掉重复的部分写在where中
你说的重复是指两条记录完成一样么,如果是则用
SELECT DISTINCT COLUMN1,COLUMN2
FROM TABLE1
或
SELECT COLUMN1,COLUMN2
FROM TABLE1
GROUP BY COLUMN1,COLUMN2
若你要查询出来的字段在两条记录中有不同值,可以用MAX()
或者可以根据你自己的要求进行排重,如下:
SELECT *
FROM (
SELECT A.*,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY ID) RN
--按ID进行归类,ORDER BY 按你自己的需要确定怎 样的记录在前面,怎样的记录在后面,因为最后的限制条件是RN = 1
FROM TABLE1 A
)
WHERE RN = 1