//循环判断第一行
for(int i=0;i<dt.Rows.Count;i++)
{
//循环判断从第二行开始与第一行比较
for(int j=i+1;j<dt.Rows.Count;j++)
{
//如果Title相同
if(dt.Rows[i][5].ToString().Equals(dt.Rows[j][5].ToString()))
{
//如果ClassID相同
if(dt.Rows[i][2].ToString().Equals(dt.Rows[j][2].ToString()))
{
//如果ChannelID相同
if(dt.Rows[i][1].ToString().Equals(dt.Rows[j][1].ToString()))
{
string Title=dt.Rows[i][5].ToString();
int ChannelID=Convert.ToInt32(dt.Rows[i][1]);
this.JDataGrid.DataSource=dt;
this.JDataGrid.DataBind();
}
//如果ChannelID不相同
else
{}
}
//如果ClassID不相同
else
{}
}
//如果Title不同
else
{}
我这里想实现的功能是在数据库实现把重复的数据提取出来.存储过程应该怎么样写呢?

解决方案 »

  1.   

    是不是这样?:
    declare @t table(id int,Title int,ClassID int,ChannelID int)
    insert @t
    select 1,1,2,3 union all
    select 2,1,2,3 union all
    select 3,2,3,4----查询Title,ClassID,ChannelID重复的行
    select * from @t as a where exists(select 1 from @t 
    where Title = a.Title and ClassID = a.ClassID and ChannelID = a.ChannelID 
    and id <> a.id)/*
    结果
    id      Title   ClassID  ChannelID
    ----------------------------------------
    1       1        2        3
    2       1        2        3
    */
      

  2.   

    hellowork(一两清风),在请问一下:
    我先从一个表里面查询出结果.
    select ArticleID,ChannelID,Content,Title,ClassID from PE_Article where ChannelID=1035
    然后在里面刷选出重复出现的数据.最后我要通过ASP.NET来调用的.
    帮我写一写..好吗?
      

  3.   

    select * from (select ArticleID,ChannelID,Content,Title,ClassID from PE_Article where ChannelID=1035) a where exists(select 1 from (select ArticleID,ChannelID,Content,Title,ClassID from PE_Article where ChannelID=1035) b
    where b.Title = a.Title and b.ClassID = a.ClassID and b.ChannelID = a.ChannelID 
    and b.id <> a.id)
      

  4.   

    SQL中没有数组变量,所以有关数组的情况可以通过表的方式来代替
    其他语句SQL基本上都可以做到
      

  5.   

    happyflystone(无枪的狙击手) 出来的结果不对啊.有些没有重复的,只有一个也会显示出来啊