本人开发一个移动的项目,功能是分配一些电路。在一个数据表(sql server)里通过通过电路号自关联形成A,B局。电路号,是在分配电路提交后,才生成的。而在分配之前是为通一为空的。
我在做分配电路的时候(还没有电路号),如何在数据表里不显示的重复的记录。另局端是不相同的字段。
比如说,数据库里面有两个记录,record1,record2.那么在inner join on 电路号的时候,就生成两条记录,record1---record2,record2---record1.而我现在只要一条记录recrecord1---record2,or record2---record1.请高手指点。在线等待!

解决方案 »

  1.   

    declare @A table (电路号 int,电路 varchar(10))
    insert @A select 1,'电路1'
    insert @A select 1,'电路2'--是笛卡尔积,4条不是2条
    select * from @A a inner join @A b
    on a.电路号=b.电路号
    /*
    1 电路1 1 电路1
    1 电路1 1 电路2
    1 电路2 1 电路1
    1 电路2 1 电路2
    */--这才是两条
    select * from @A a inner join @A b
    on a.电路号=b.电路号
    where a.电路<>b.电路
    /*
    1 电路1 1 电路2
    1 电路2 1 电路1
    */--这是1条
    select * from @A a inner join @A b
    on a.电路号=b.电路号
    where a.电路<b.电路
    /*
    1 电路1 1 电路2
    */--这也是1条
    select * from @A a inner join @A b
    on a.电路号=b.电路号
    where a.电路>b.电路
    /*
    1 电路2 1 电路1
    */
      

  2.   

    这种方法不行,我的电路号是为空的字段,因为现在还没有分配电路号,电路号要等分配提交后才能生成。就是当用为空的字段关联的时候,如何实现这种效果,如:
    record1---record2,record2---record1.而我现在只要一条记录recrecord1---record2,or record2---record1.请高手指点。
      

  3.   

    电路号为NULL,那用什么条件 INNER JOIN,也就是 ON 什么
      

  4.   

    电种号,有两种情况,一种是有的,一种是为空的。我现在是为空的电路号,想形成分配,让它自关联形成a,b局,然后当他提交的时候(jsp)就形成电路号。String sql = "select top "+ qunti+" * from switch as a inner join switch as b on a.ELEC_LINE_NUM=b.ELEC_LINE_NUM where a.port_of_buearo = '"+ title + "' and b.port_of_buearo = '"+author+ "' and a.ELEC_ATTRI like '%未启用%' and b.ELEC_ATTRI like '%未启用%' and a.MODEL_OF = '"+content+"' and b.MODEL_OF = '"+content+"'";
      

  5.   

    on a.ELEC_LINE_NUM=b.ELEC_LINE_NUM
    -----------------
    ELEC_LINE_NUM 是电路号? NULL?
      

  6.   

    ELEC_LINE_NUM是电路号,ELEC_LINE_NUM="",
      

  7.   

    ... inner join switch as b on a.ELEC_LINE_NUM='' and b.ELEC_LINE_NUM='' where a.电路<b.电路 ...-- OR... inner join switch as b on a.ELEC_LINE_NUM='' and b.ELEC_LINE_NUM='' where a.电路>b.电路 ...
      

  8.   

    其实不太清楚你的需求,是任意配对捏还是一对一配对。如果是一对一配对,不管电路号时如何生成的,应该有个配对号(多加一列)才对,是吧。如果是将 ELEC_LINE_NUM='' 的任意配对,上面应该能满足要求。
      

  9.   


    declare @A table(KID int,PF varchar(2),COLT int,SIZET varchar(1),QUANTITY int,sh int)
    insert @A
    select 1,'AA',51,'M',20,1 union all
    select 2,'AA',51,'M',20,0 union all
    select 3,'AA',51,'M',40,1 union all
    select 4,'AA',51,'M',40,1
    select * from @a a where kid=(select top 1 kid from @a b where a.pf=b.pf and a.sizet=b.sizet order by newid())
    /*
    (所影响的行数为 4 行)KID         PF   COLT        SIZET QUANTITY    sh          
    ----------- ---- ----------- ----- ----------- ----------- 
    2           AA   51          M     20          0(所影响的行数为 1 行)
    */