【求助]如何不显示重复的数据??

时间:2023-01-09 09:24:54

--19647 198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目
--19647 197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题


 如上面语句
aid 这个字段数据重复了
为19647
怎么去重复。

并且只有AID重复
其余都不重复。
所以其余的还是需要取出来?
怎么实现?

71 个解决方案

#1


那你想要什么结果

SELECT * FROM TB T WHERE COL=(SELECT MAX(COL) FROM TB WHERE AID=T.AID)

SELECT * FROM TB T WHERE COL=(SELECT MIN(COL) FROM TB WHERE AID=T.AID)

#2



SELECT * FROM xskj_attachments T WHERE aid=(SELECT MAX(aid) FROM xskj_attachments WHERE AID=T.AID)

这条语句执行完之后数据还是重复。

--194 19645 UpLoadFiles/Doc/3042890985979332.doc 2010-02-01 17:30:42.890 程序员测试题目
195 19646 UpLoadFiles/Doc/380406235050971.doc 2010-02-01 17:38:00.470 _IT测试题
196 19646 UpLoadFiles/Doc/38046832455202.doc 2010-02-01 17:38:00.470 需求资料
197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题
198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目


#3


select * from tb b where  not exists (select * from tb where b.aid=aid and b.col>col)

#4


引用 3 楼 ws_hgo 的回复:
SQL codeselect*from tb bwherenotexists (select*from tbwhere b.aid=aidand b.col>col)
还是会重复?

#5


----------------------------------------------------------------
-- Author  :SQL77(只为思齐老)
-- Date    :2010-02-02 08:36:02
-- Version:
--      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
-- Aug  6 2000 00:57:48 
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go 
create table #TB([A] int,[C2] int,[C3] varchar(35),[C4] datetime,[C5] varchar(14))
insert #TB
select 195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题' union all
select 196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料' union all
select 197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题' union all
select 198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'
--------------开始查询--------------------------

select * from #TB T WHERE A=(SELECT MAX(A) FROM #TB WHERE C2=T.C2)

select * from #TB T WHERE A=(SELECT MIN(A) FROM #TB WHERE C2=T.C2)
----------------结果----------------------------
/* 

(所影响的行数为 4 行)

A           C2          C3                                  C4                                                     C5             
----------- ----------- ----------------------------------- ------------------------------------------------------ -------------- 
198         19647       UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657                                程序员测试题目
196         19646       UpLoadFiles/Doc/38046832455202.doc  2010-02-01 17:38:00.470                                需求资料

(所影响的行数为 2 行)

A           C2          C3                                  C4                                                     C5             
----------- ----------- ----------------------------------- ------------------------------------------------------ -------------- 
195         19646       UpLoadFiles/Doc/380406235050971.doc 2010-02-01 17:38:00.470                                _IT测试题
197         19647       UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657                                _IT测试题

(所影响的行数为 2 行)


*/

#6


引用 4 楼 xupeihuagudulei 的回复:
引用 3 楼 ws_hgo 的回复:
SQL codeselect*from tb bwherenotexists (select*from tbwhere b.aid=aidand b.col>col)
还是会重复?

你的列错了,AID本来就相同,在相同里面的最大当然是相同啦,呵呵

#7


http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html?63394

#8


--1、
select * from tb t where col=(select max(col) from tb where aid=t.aid)
--2、
select * from tb t where col=(select min(col) from tb where aid=t.aid)
--3、
select * from tb b where  not exists (select * from tb where b.aid=aid and b.col>col)

#9


if object_id('tempdb.dbo.#TB') is not null drop table #TB
go 
create table #TB([A] int,[C2] int,[C3] varchar(35),[C4] datetime,[C5] varchar(14))
insert #TB
select 195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题' union all
select 196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料' union all
select 197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题' union all
select 198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'


select * from #TB b where  not exists (select * from #TB where b.[C2]=[C2] and b.[A]>[A])


A           C2          C3                                  C4                      C5
----------- ----------- ----------------------------------- ----------------------- --------------
195         19646       UpLoadFiles/Doc/380406235050971.doc 2010-02-01 17:38:00.470 _IT测试题
197         19647       UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题

(2 行受影响)

#10


那我先把我所有情况都说说吧
一张文章表,
一张附件表
一张文章表里可能会有多个附件,
然后对应到数据库的结果为下面所示

select * from xskj_article where [id]=19647

--数据为:19647 fdsfdsfs <p>fdsfds</p> Null 2010-02-01 17:39:08.657 31 NULL NULL NULL


这个文章添加了两个附件,
附件表的数据为

select * from xskj_attachments where [aid]=19647

--197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题
--198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目

#11


引用 4 楼 xupeihuagudulei 的回复:
引用 3 楼 ws_hgo 的回复:
SQL codeselect*from tb bwherenotexists (select*from tbwhere b.aid=aidand b.col>col)
还是会重复?

你的列弄错啦!~

#12


然后用我的语句显示的话就会出现这个效果
【求助]如何不显示重复的数据??
重复了。。

#13


是不是
只取一个附件的
那么上面的就满足

#14


引用 9 楼 ws_hgo 的回复:
SQL codeifobject_id('tempdb.dbo.#TB')isnotnulldroptable #TBgocreatetable #TB([A]int,[C2]int,[C3]varchar(35),[C4]datetime,[C5]varchar(14))insert #TBselect195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题'unionallselect196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料'unionallselect197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题'unionallselect198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'select*from #TB bwherenotexists (select*from #TBwhere b.[C2]=[C2]and b.[A]>[A])


A           C2          C3                                  C4                      C5----------- ----------- ----------------------------------- ----------------------- --------------19519646       UpLoadFiles/Doc/380406235050971.doc2010-02-0117:38:00.470 _IT测试题19719647       UpLoadFiles/Doc/398656762743851.doc2010-02-0117:39:08.657 _IT测试题

(2 行受影响)
恩,一个是附件表里的ID
一个是文章表的ID
囧~!
 我两个都写了一样对吧

#15


引用 13 楼 ws_hgo 的回复:
是不是
 只取一个附件的
 那么上面的就满足
附件因为页面不用显示
是通过传递aid到另一个页面的
所以不管附件也没关系
要是真要管附件估计更难办了

因为你给出来的语句只会显示一个附件

#16


引用 14 楼 xupeihuagudulei 的回复:
引用 9 楼 ws_hgo 的回复:
SQL codeifobject_id('tempdb.dbo.#TB')isnotnulldroptable #TBgocreatetable #TB([A]int,[C2]int,[C3]varchar(35),[C4]datetime,[C5]varchar(14))insert #TBselect195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题'unionallselect196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料'unionallselect197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题'unionallselect198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'select*from #TB bwherenotexists (select*from #TBwhere b.[C2]=[C2]and b.[A]>[A])


A          C2          C3                                  C4                      C5----------- ----------- ----------------------------------- ----------------------- --------------19519646      UpLoadFiles/Doc/380406235050971.doc2010-02-0117:38:00.470 _IT测试题19719647      UpLoadFiles/Doc/398656762743851.doc2010-02-0117:39:08.657 _IT测试题

(2 行受影响)
恩,一个是附件表里的ID
一个是文章表的ID
囧~!
我两个都写了一样对吧

一个文章对应多个附件是不是
那么写sql的时候
not exists (select * from #TB where b.[C2]=[C2] and b.[A]>[A])--》A就是文章的ID,C2是附建的ID

#17


还是不行?

select * from xskj_attachments b where not exists(select * from xskj_attachments where b.aid=aid and b.[id]>b.[id])
数据:197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题
198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目

#18


说反拉
A就是附建的ID,C2是文章的ID

#19


试下:

select * from xskj_attachments b where not exists(select * from xskj_attachments where b.aid>aid and b.[id]=b.[id])

#20


那我上面的语句和你的没错啊
aid是文章ID
id是附件表的ID
可是出来的数据就是这样的

#21



又写错啦

select * from xskj_attachments b where not exists(select * from xskj_attachments where b.aid=aid and b.[id]>[id])-->后面的b.[id]还成[id]

#22


引用 20 楼 xupeihuagudulei 的回复:
那我上面的语句和你的没错啊
aid是文章ID
id是附件表的ID
可是出来的数据就是这样的

用21楼的

#23


引用 19 楼 ws_hgo 的回复:
试下:

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid>aidand b.[id]=b.[id])
我两种 ID都换过试了
结果都不行
【求助]如何不显示重复的数据??

#24


select * from xskj_attachments b where 
not exists(select * from xskj_attachments where b.aid=aid and b.[id]>b.[id])

上面的是你的后面的b.id>b.id是错的
换成b.id>id
就可以啦

#25


引用 23 楼 xupeihuagudulei 的回复:
引用 19 楼 ws_hgo 的回复:
试下:

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid>aidand b.[id]=b.[id])
我两种 ID都换过试了
结果都不行

看21楼的
你错误
见我24楼的描述

#26


引用 21 楼 ws_hgo 的回复:

 又写错啦

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid=aidand b.[id]>[id])-->后面的b.[id]还成[id]
这个对了。


你是不是评论博客太多了。
这都 一直搞错。

b.id是子查询里面的id 
而id是外面那个查询的ID对吧

#27


引用 26 楼 xupeihuagudulei 的回复:
引用 21 楼 ws_hgo 的回复:

又写错啦

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid=aidand b.[id]>[id])-->后面的b.[id]还成[id]
这个对了。


你是不是评论博客太多了。
这都 一直搞错。

b.id是子查询里面的id
而id是外面那个查询的ID对吧

呵呵
刚才没有看清楚
冒得问题块结贴
哈哈~~~

#29


==,有问题再问下
因为这语句还需要和两张表的连接查询出来的
我还不知道咋放进去。
囧。刚刚试了下出问题了
我再去看看

#30



DECLARE @indextable TABLE(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) SELECT [ID] FROM [xskj_article] WHERE[Fid]=33 order by [Posttime] DESC,[ID] DESC

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM [xskj_article] A  inner join @indextable t on A.[id]=t.[nid] 
left join xskj_attachments C on c.[aid]=a.[id]
WHERE not exists(select * from attachments t where t.aid=C.aid and t.[id]>C.[id]) and  A.[Fid]=33 and t.[id] between @startIndex and @endIndex order by a.posttime desc, a.[id] desc

看看这么写对不对
放到程序里就出错了。
存储过程没错。
可是在asp.net中运行就报错了
而且是莫名的错误。

#31


asp.net饿错误贴出来
我看下

#32


引用 31 楼 ws_hgo 的回复:
asp.net饿错误贴出来
 我看下
等会
我还在看。好像是语句写错了
是xskj_attachments
但是where 里面写的是attachmennts

#33


不过这么一写,一条数据都没了。囧。

#34


直接在
sql里面执行是不是你想要的结果啊

#35


现在没有错误,不过一条数据都没有

#36


引用 32 楼 xupeihuagudulei 的回复:
引用 31 楼 ws_hgo 的回复:
asp.net饿错误贴出来
我看下
等会
我还在看。好像是语句写错了
是xskj_attachments
但是where 里面写的是attachmennts

你仔细检查下sql

#37



CREATE PROCEDURE P_GetPagedPatentDeclarate
(
  @startIndex INT,
@endIndex INT
)
 AS
SET NOCOUNT ON
DECLARE @indextable TABLE(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) SELECT [ID] FROM [xskj_article] WHERE[Fid]=33 order by [Posttime] DESC,[ID] DESC

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM [xskj_article] A  inner join @indextable t on A.[id]=t.[nid] 
left join xskj_attachments C on c.[aid]=a.[id]
WHERE not exists(select * from xskj_attachments t where t.aid=t.aid and t.[id]>C.[id]) and  A.[Fid]=33 and t.[id] between @startIndex and @endIndex order by a.posttime desc, a.[id] desc
set nocount off
RETURN
GO


上面是存储过程
asp.net中就一个repeater,一个分页控件aspnetpager
然后用存储过程绑定
后台代码贴下

     void bindData()
        {
            myrepeater.DataSource = SqlHelper.ExecuteReader(SqlStr.xskjConnectString, CommandType.StoredProcedure, "P_GetPagedPatentDeclarate",
                   new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
                   new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
            myrepeater.DataBind();

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindData();
        }

   if (!IsPostBack)
            {
                int total = (int)SqlHelper.ExecuteScalar(SqlStr.xskjConnectString, CommandType.StoredProcedure, "P_GetPatentDeclarateNumber");
                AspNetPager1.RecordCount = total;
            }
我认为是sql没写对
囧。帮偶看看吧,
这语句超过这么多,偶看的就头痛了

#38


是sql问题
简化一下就是这样的语句

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM [xskj_article] A  
left join xskj_attachments C on c.[aid]=a.[id]
WHERE not exists(select * from xskj_attachments t where t.aid=t.aid and t.[id]>C.[id]) and  A.[Fid]=33 order by a.posttime desc, a.[id] desc

一条数据都没有。这样
应该没错啊、

#39


大致看了下
没有问题
直接上表结构
测试数据及其结果吧

#40


我靠。。。晕死

#41


外部查询要加个where 我上面写的那样对吗?

#42


用这个试下

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM 
(
  select * from [xskj_article] where [Fid]=33
)
 A  
left join 
(
  select * from xskj_attachments t where t.aid=t.aid and t.[id]>C.[id]
)
C
on c.[aid]=a.[id]
order by a.posttime desc, a.[id] desc

#43


是后面where fid=33的关系
xskj_article里面没有fid=33的数据 
可是我没有把那些改过的语句放进去之前 
存储过程这个条件fid=33也有的
但前面都有数据出来。
如图
【求助]如何不显示重复的数据??

#44


在表中再建一自动序号列,唯一ID 用这个再进行查询统计,保证不会还重复.

#45


逐句测试你的存储过程吧,看在哪一步与你要的结果不同

#46


引用 42 楼 ws_hgo 的回复:
用这个试下
SQL codeSELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=casewhenisnull([filename],'')=''then'No'else'Yes'endFROM 
(select*from[xskj_article]where[Fid]=33
)
 Aleftjoin 
(select*from xskj_attachments twhere t.aid=t.aidand t.[id]>C.[id]
)
Con c.[aid]=a.[id]orderby a.posttimedesc, a.[id]desc
我现在已经差不多弄对了
不过之前那些数据都不出来了
现在新增加的话会显示的
真奇怪。

#47


我再测试测试
应该对了。 

#48


修改下



SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM 
(
  select * from [xskj_article] where [Fid]=33
)
 A  
left join 
(
  select * from xskj_attachments t where not exists(select * from xskj_attachments where t.aid=aid and t.[id]>[id])
)
C
on c.[aid]=a.[id]
order by a.posttime desc, a.[id] desc

#49


用我
48楼的
看看
我先撤啦
有事情call 我

#50


现在显示的数据是对了
可是显示数量不一样
囧~!
asp.net中只会显示最后插入的一条记录
如图
【求助]如何不显示重复的数据??
就如这张图
目前不是有几条记录吗?
但是我现在新增加的话
就只会显示一条
,而且新增一条,就后面新增的把前面的覆盖了

#1


那你想要什么结果

SELECT * FROM TB T WHERE COL=(SELECT MAX(COL) FROM TB WHERE AID=T.AID)

SELECT * FROM TB T WHERE COL=(SELECT MIN(COL) FROM TB WHERE AID=T.AID)

#2



SELECT * FROM xskj_attachments T WHERE aid=(SELECT MAX(aid) FROM xskj_attachments WHERE AID=T.AID)

这条语句执行完之后数据还是重复。

--194 19645 UpLoadFiles/Doc/3042890985979332.doc 2010-02-01 17:30:42.890 程序员测试题目
195 19646 UpLoadFiles/Doc/380406235050971.doc 2010-02-01 17:38:00.470 _IT测试题
196 19646 UpLoadFiles/Doc/38046832455202.doc 2010-02-01 17:38:00.470 需求资料
197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题
198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目


#3


select * from tb b where  not exists (select * from tb where b.aid=aid and b.col>col)

#4


引用 3 楼 ws_hgo 的回复:
SQL codeselect*from tb bwherenotexists (select*from tbwhere b.aid=aidand b.col>col)
还是会重复?

#5


----------------------------------------------------------------
-- Author  :SQL77(只为思齐老)
-- Date    :2010-02-02 08:36:02
-- Version:
--      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
-- Aug  6 2000 00:57:48 
-- Copyright (c) 1988-2000 Microsoft Corporation
-- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#TB
if object_id('tempdb.dbo.#TB') is not null drop table #TB
go 
create table #TB([A] int,[C2] int,[C3] varchar(35),[C4] datetime,[C5] varchar(14))
insert #TB
select 195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题' union all
select 196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料' union all
select 197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题' union all
select 198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'
--------------开始查询--------------------------

select * from #TB T WHERE A=(SELECT MAX(A) FROM #TB WHERE C2=T.C2)

select * from #TB T WHERE A=(SELECT MIN(A) FROM #TB WHERE C2=T.C2)
----------------结果----------------------------
/* 

(所影响的行数为 4 行)

A           C2          C3                                  C4                                                     C5             
----------- ----------- ----------------------------------- ------------------------------------------------------ -------------- 
198         19647       UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657                                程序员测试题目
196         19646       UpLoadFiles/Doc/38046832455202.doc  2010-02-01 17:38:00.470                                需求资料

(所影响的行数为 2 行)

A           C2          C3                                  C4                                                     C5             
----------- ----------- ----------------------------------- ------------------------------------------------------ -------------- 
195         19646       UpLoadFiles/Doc/380406235050971.doc 2010-02-01 17:38:00.470                                _IT测试题
197         19647       UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657                                _IT测试题

(所影响的行数为 2 行)


*/

#6


引用 4 楼 xupeihuagudulei 的回复:
引用 3 楼 ws_hgo 的回复:
SQL codeselect*from tb bwherenotexists (select*from tbwhere b.aid=aidand b.col>col)
还是会重复?

你的列错了,AID本来就相同,在相同里面的最大当然是相同啦,呵呵

#7


http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html?63394

#8


--1、
select * from tb t where col=(select max(col) from tb where aid=t.aid)
--2、
select * from tb t where col=(select min(col) from tb where aid=t.aid)
--3、
select * from tb b where  not exists (select * from tb where b.aid=aid and b.col>col)

#9


if object_id('tempdb.dbo.#TB') is not null drop table #TB
go 
create table #TB([A] int,[C2] int,[C3] varchar(35),[C4] datetime,[C5] varchar(14))
insert #TB
select 195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题' union all
select 196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料' union all
select 197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题' union all
select 198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'


select * from #TB b where  not exists (select * from #TB where b.[C2]=[C2] and b.[A]>[A])


A           C2          C3                                  C4                      C5
----------- ----------- ----------------------------------- ----------------------- --------------
195         19646       UpLoadFiles/Doc/380406235050971.doc 2010-02-01 17:38:00.470 _IT测试题
197         19647       UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题

(2 行受影响)

#10


那我先把我所有情况都说说吧
一张文章表,
一张附件表
一张文章表里可能会有多个附件,
然后对应到数据库的结果为下面所示

select * from xskj_article where [id]=19647

--数据为:19647 fdsfdsfs <p>fdsfds</p> Null 2010-02-01 17:39:08.657 31 NULL NULL NULL


这个文章添加了两个附件,
附件表的数据为

select * from xskj_attachments where [aid]=19647

--197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题
--198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目

#11


引用 4 楼 xupeihuagudulei 的回复:
引用 3 楼 ws_hgo 的回复:
SQL codeselect*from tb bwherenotexists (select*from tbwhere b.aid=aidand b.col>col)
还是会重复?

你的列弄错啦!~

#12


然后用我的语句显示的话就会出现这个效果
【求助]如何不显示重复的数据??
重复了。。

#13


是不是
只取一个附件的
那么上面的就满足

#14


引用 9 楼 ws_hgo 的回复:
SQL codeifobject_id('tempdb.dbo.#TB')isnotnulldroptable #TBgocreatetable #TB([A]int,[C2]int,[C3]varchar(35),[C4]datetime,[C5]varchar(14))insert #TBselect195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题'unionallselect196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料'unionallselect197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题'unionallselect198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'select*from #TB bwherenotexists (select*from #TBwhere b.[C2]=[C2]and b.[A]>[A])


A           C2          C3                                  C4                      C5----------- ----------- ----------------------------------- ----------------------- --------------19519646       UpLoadFiles/Doc/380406235050971.doc2010-02-0117:38:00.470 _IT测试题19719647       UpLoadFiles/Doc/398656762743851.doc2010-02-0117:39:08.657 _IT测试题

(2 行受影响)
恩,一个是附件表里的ID
一个是文章表的ID
囧~!
 我两个都写了一样对吧

#15


引用 13 楼 ws_hgo 的回复:
是不是
 只取一个附件的
 那么上面的就满足
附件因为页面不用显示
是通过传递aid到另一个页面的
所以不管附件也没关系
要是真要管附件估计更难办了

因为你给出来的语句只会显示一个附件

#16


引用 14 楼 xupeihuagudulei 的回复:
引用 9 楼 ws_hgo 的回复:
SQL codeifobject_id('tempdb.dbo.#TB')isnotnulldroptable #TBgocreatetable #TB([A]int,[C2]int,[C3]varchar(35),[C4]datetime,[C5]varchar(14))insert #TBselect195,19646,'UpLoadFiles/Doc/380406235050971.doc','2010-02-01 17:38:00.470','_IT测试题'unionallselect196,19646,'UpLoadFiles/Doc/38046832455202.doc','2010-02-01 17:38:00.470','需求资料'unionallselect197,19647,'UpLoadFiles/Doc/398656762743851.doc','2010-02-01 17:39:08.657','_IT测试题'unionallselect198,19647,'UpLoadFiles/Doc/398656853104902.doc','2010-02-01 17:39:08.657','程序员测试题目'select*from #TB bwherenotexists (select*from #TBwhere b.[C2]=[C2]and b.[A]>[A])


A          C2          C3                                  C4                      C5----------- ----------- ----------------------------------- ----------------------- --------------19519646      UpLoadFiles/Doc/380406235050971.doc2010-02-0117:38:00.470 _IT测试题19719647      UpLoadFiles/Doc/398656762743851.doc2010-02-0117:39:08.657 _IT测试题

(2 行受影响)
恩,一个是附件表里的ID
一个是文章表的ID
囧~!
我两个都写了一样对吧

一个文章对应多个附件是不是
那么写sql的时候
not exists (select * from #TB where b.[C2]=[C2] and b.[A]>[A])--》A就是文章的ID,C2是附建的ID

#17


还是不行?

select * from xskj_attachments b where not exists(select * from xskj_attachments where b.aid=aid and b.[id]>b.[id])
数据:197 19647 UpLoadFiles/Doc/398656762743851.doc 2010-02-01 17:39:08.657 _IT测试题
198 19647 UpLoadFiles/Doc/398656853104902.doc 2010-02-01 17:39:08.657 程序员测试题目

#18


说反拉
A就是附建的ID,C2是文章的ID

#19


试下:

select * from xskj_attachments b where not exists(select * from xskj_attachments where b.aid>aid and b.[id]=b.[id])

#20


那我上面的语句和你的没错啊
aid是文章ID
id是附件表的ID
可是出来的数据就是这样的

#21



又写错啦

select * from xskj_attachments b where not exists(select * from xskj_attachments where b.aid=aid and b.[id]>[id])-->后面的b.[id]还成[id]

#22


引用 20 楼 xupeihuagudulei 的回复:
那我上面的语句和你的没错啊
aid是文章ID
id是附件表的ID
可是出来的数据就是这样的

用21楼的

#23


引用 19 楼 ws_hgo 的回复:
试下:

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid>aidand b.[id]=b.[id])
我两种 ID都换过试了
结果都不行
【求助]如何不显示重复的数据??

#24


select * from xskj_attachments b where 
not exists(select * from xskj_attachments where b.aid=aid and b.[id]>b.[id])

上面的是你的后面的b.id>b.id是错的
换成b.id>id
就可以啦

#25


引用 23 楼 xupeihuagudulei 的回复:
引用 19 楼 ws_hgo 的回复:
试下:

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid>aidand b.[id]=b.[id])
我两种 ID都换过试了
结果都不行

看21楼的
你错误
见我24楼的描述

#26


引用 21 楼 ws_hgo 的回复:

 又写错啦

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid=aidand b.[id]>[id])-->后面的b.[id]还成[id]
这个对了。


你是不是评论博客太多了。
这都 一直搞错。

b.id是子查询里面的id 
而id是外面那个查询的ID对吧

#27


引用 26 楼 xupeihuagudulei 的回复:
引用 21 楼 ws_hgo 的回复:

又写错啦

SQL codeselect*from xskj_attachments bwherenotexists(select*from xskj_attachmentswhere b.aid=aidand b.[id]>[id])-->后面的b.[id]还成[id]
这个对了。


你是不是评论博客太多了。
这都 一直搞错。

b.id是子查询里面的id
而id是外面那个查询的ID对吧

呵呵
刚才没有看清楚
冒得问题块结贴
哈哈~~~

#28


#29


==,有问题再问下
因为这语句还需要和两张表的连接查询出来的
我还不知道咋放进去。
囧。刚刚试了下出问题了
我再去看看

#30



DECLARE @indextable TABLE(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) SELECT [ID] FROM [xskj_article] WHERE[Fid]=33 order by [Posttime] DESC,[ID] DESC

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM [xskj_article] A  inner join @indextable t on A.[id]=t.[nid] 
left join xskj_attachments C on c.[aid]=a.[id]
WHERE not exists(select * from attachments t where t.aid=C.aid and t.[id]>C.[id]) and  A.[Fid]=33 and t.[id] between @startIndex and @endIndex order by a.posttime desc, a.[id] desc

看看这么写对不对
放到程序里就出错了。
存储过程没错。
可是在asp.net中运行就报错了
而且是莫名的错误。

#31


asp.net饿错误贴出来
我看下

#32


引用 31 楼 ws_hgo 的回复:
asp.net饿错误贴出来
 我看下
等会
我还在看。好像是语句写错了
是xskj_attachments
但是where 里面写的是attachmennts

#33


不过这么一写,一条数据都没了。囧。

#34


直接在
sql里面执行是不是你想要的结果啊

#35


现在没有错误,不过一条数据都没有

#36


引用 32 楼 xupeihuagudulei 的回复:
引用 31 楼 ws_hgo 的回复:
asp.net饿错误贴出来
我看下
等会
我还在看。好像是语句写错了
是xskj_attachments
但是where 里面写的是attachmennts

你仔细检查下sql

#37



CREATE PROCEDURE P_GetPagedPatentDeclarate
(
  @startIndex INT,
@endIndex INT
)
 AS
SET NOCOUNT ON
DECLARE @indextable TABLE(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) SELECT [ID] FROM [xskj_article] WHERE[Fid]=33 order by [Posttime] DESC,[ID] DESC

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM [xskj_article] A  inner join @indextable t on A.[id]=t.[nid] 
left join xskj_attachments C on c.[aid]=a.[id]
WHERE not exists(select * from xskj_attachments t where t.aid=t.aid and t.[id]>C.[id]) and  A.[Fid]=33 and t.[id] between @startIndex and @endIndex order by a.posttime desc, a.[id] desc
set nocount off
RETURN
GO


上面是存储过程
asp.net中就一个repeater,一个分页控件aspnetpager
然后用存储过程绑定
后台代码贴下

     void bindData()
        {
            myrepeater.DataSource = SqlHelper.ExecuteReader(SqlStr.xskjConnectString, CommandType.StoredProcedure, "P_GetPagedPatentDeclarate",
                   new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
                   new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
            myrepeater.DataBind();

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindData();
        }

   if (!IsPostBack)
            {
                int total = (int)SqlHelper.ExecuteScalar(SqlStr.xskjConnectString, CommandType.StoredProcedure, "P_GetPatentDeclarateNumber");
                AspNetPager1.RecordCount = total;
            }
我认为是sql没写对
囧。帮偶看看吧,
这语句超过这么多,偶看的就头痛了

#38


是sql问题
简化一下就是这样的语句

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM [xskj_article] A  
left join xskj_attachments C on c.[aid]=a.[id]
WHERE not exists(select * from xskj_attachments t where t.aid=t.aid and t.[id]>C.[id]) and  A.[Fid]=33 order by a.posttime desc, a.[id] desc

一条数据都没有。这样
应该没错啊、

#39


大致看了下
没有问题
直接上表结构
测试数据及其结果吧

#40


我靠。。。晕死

#41


外部查询要加个where 我上面写的那样对吗?

#42


用这个试下

SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM 
(
  select * from [xskj_article] where [Fid]=33
)
 A  
left join 
(
  select * from xskj_attachments t where t.aid=t.aid and t.[id]>C.[id]
)
C
on c.[aid]=a.[id]
order by a.posttime desc, a.[id] desc

#43


是后面where fid=33的关系
xskj_article里面没有fid=33的数据 
可是我没有把那些改过的语句放进去之前 
存储过程这个条件fid=33也有的
但前面都有数据出来。
如图
【求助]如何不显示重复的数据??

#44


在表中再建一自动序号列,唯一ID 用这个再进行查询统计,保证不会还重复.

#45


逐句测试你的存储过程吧,看在哪一步与你要的结果不同

#46


引用 42 楼 ws_hgo 的回复:
用这个试下
SQL codeSELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=casewhenisnull([filename],'')=''then'No'else'Yes'endFROM 
(select*from[xskj_article]where[Fid]=33
)
 Aleftjoin 
(select*from xskj_attachments twhere t.aid=t.aidand t.[id]>C.[id]
)
Con c.[aid]=a.[id]orderby a.posttimedesc, a.[id]desc
我现在已经差不多弄对了
不过之前那些数据都不出来了
现在新增加的话会显示的
真奇怪。

#47


我再测试测试
应该对了。 

#48


修改下



SELECT A.[ID], A.[Title], A.[Content], A.[Posttime], A.[Image], A.[views],C.*,
att=case when isnull([filename],'') = '' then 'No' else 'Yes' end
 FROM 
(
  select * from [xskj_article] where [Fid]=33
)
 A  
left join 
(
  select * from xskj_attachments t where not exists(select * from xskj_attachments where t.aid=aid and t.[id]>[id])
)
C
on c.[aid]=a.[id]
order by a.posttime desc, a.[id] desc

#49


用我
48楼的
看看
我先撤啦
有事情call 我

#50


现在显示的数据是对了
可是显示数量不一样
囧~!
asp.net中只会显示最后插入的一条记录
如图
【求助]如何不显示重复的数据??
就如这张图
目前不是有几条记录吗?
但是我现在新增加的话
就只会显示一条
,而且新增一条,就后面新增的把前面的覆盖了