sql 查询去除重复行

sql 查询去除重复行,第1张

首先,从img表中取数据库,将new_id重复的过滤掉,代码

select

min(id)

from

img

group

by

new_id

------以new_id字段分组,取最小的ID,这个ID总不会重复了吧

然后将这个查询结果以虚拟表形式,作为过滤条件,取你所要的结果,代码为

select

Tnew_id

AS

is,title,d_time,imgurl

from

news,Img

where

newsid

=

imgnew_id

and

imgid

in

(select

min(id)

AS

img_id,new_id

from

img

group

by

new_id)

sql="select distinct (tzid),tztitle from tz,cmt where cmtuid='"+当前用户ID+"'" and cmttieziID=tzid;

这行就行了 实际上就是 把贴子的ID 前加个 distintc

作用: 剔除重复语句

declare @t table (ID int, Name varchar(10), address varchar(10))

insert into @t select 1,'a','1'

insert into @t select 2,'a','2'

insert into @t select 3,'a','1'

insert into @t select 4,'a','3'

insert into @t select 5,'b','2'

insert into @t select 6,'b','2'

insert into @t select 7,'b','3'

insert into @t select 8,'b','1'

insert into @t select 9,'c','1'

insert into @t select 10,'d','2'

select ID,Name,address from

(

select ,orderid=(select count(1) from @t where name=xname and address=xaddress)

from @t as x

) as y

where orderid>=2

--结果

/

ID Name address

----------------------------------

1 a 1

3 a 1

5 b 2

6 b 2

/

--没试过我的?我的测试通过啊!!!

补充:

看不懂?

很简单的呀,就这样:

select ID,Name,address from

(

select ,orderid=(select count(1) from 表名 where name=xname and address=xaddress)

from 表名 as x

) as y

where orderid>=2

MySQL 过滤重复数据

有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据。

如果你需要读取不重复的数据可以在 SELECT 语句中使用 DISTINCT 关键字来过滤重复数据。

from 树懒学堂- 一站式数据知识学习平台

你也可以使用 GROUP BY 来读取数据表中不重复的数据:

insert

into

Person('姓名','年龄')

select

s姓名,s年龄

from

Student

s

where

not

exists

(select

1

from

Person

p,Student

ss

where

p姓名=ss姓名

and

p年龄=ss年龄)

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

扩展资料:

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)

以上就是关于sql 查询去除重复行全部的内容,包括:sql 查询去除重复行、求一个SQL语句,要求过滤掉重复的记录,谢谢!、sql怎样过滤重复记录等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9675865.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存