有两个表:
 表1                    
字段A 
 12
123  表2
字段A
122456
123456
123666请问我想分析表2的数据,得出结果它们分别属于表一的哪一个数据。注意,表一的数据有重叠,不能单纯的用赋值语句。例如:表二中,123456只属于123不属于12。

解决方案 »

  1.   

    select
        *
    from 
        t1 a,t2 b 
    where
        a.字段A=left(b.字段A,len(a.字段A))
        and
        not exists(select 1 from t1 where len(字段A)>len(a.字段A) and 字段A=left(b.字段A,len(字段A)))
      

  2.   

    create table t1(字段A varchar(20))
    insert into t1 select '12'
    insert into t1 select '123' create table t2(字段A varchar(20))
    insert into t2 select '122456'
    insert into t2 select '123456'
    insert into t2 select '123666'
    goselect
        *
    from 
        t1 a,t2 b 
    where
        a.字段A=left(b.字段A,len(a.字段A))
        and
        not exists(select 1 from t1 where len(字段A)>len(a.字段A) and 字段A=left(b.字段A,len(字段A)))
    go/*
    字段A                字段A                  
    -------------------- -------------------- 
    12                   122456
    123                  123456
    123                  123666
    */drop table t1,t2
    go
      

  3.   

    create table t1(A varchar(10))
    insert into t1 values('12')
    insert into t1 values('123')
    create table t2(B varchar(10))
    insert into t2 values('122456')
    insert into t2 values('123456')
    insert into t2 values('123666')
    insert into t2 values('122666')select m1.a,m1.b from
    (
      select t1.a,t2.b,len(t1.a) my_len from t1,t2 where charindex(t1.a,t2.b) > 0
    ) m1,
    (
      select b,max(my_len) my_len from 
      (
        select t1.a,t2.b,len(t1.a) my_len from t1,t2 where charindex(t1.a,t2.b) > 0
      ) t
      group by b
    ) m2
    where m1.b = m2.b and m1.my_len = m2.my_lendrop table t1, t2/*
    a          b          
    ---------- ---------- 
    12         122456
    12         122666
    123        123456
    123        123666(所影响的行数为 4 行)
    */
      

  4.   

    --我上面多了条测试数据,下为你原来的测试数据.create table t1(A varchar(10))
    insert into t1 values('12')
    insert into t1 values('123')
    create table t2(B varchar(10))
    insert into t2 values('122456')
    insert into t2 values('123456')
    insert into t2 values('123666')select m1.a,m1.b from
    (
      select t1.a,t2.b,len(t1.a) my_len from t1,t2 where charindex(t1.a,t2.b) > 0
    ) m1,
    (
      select b,max(my_len) my_len from 
      (
        select t1.a,t2.b,len(t1.a) my_len from t1,t2 where charindex(t1.a,t2.b) > 0
      ) t
      group by b
    ) m2
    where m1.b = m2.b and m1.my_len = m2.my_lendrop table t1, t2/*
    a          b          
    ---------- ---------- 
    12         122456
    123        123456
    123        123666
    (所影响的行数为 3 行)
    */
      

  5.   

    create table t1(a1 varchar(20))
    insert into t1 select '12'
    insert into t1 select '123'create table t2(a2 varchar(20))
    insert into t2 select '122456'
    insert into t2 select '123456'
    insert into t2 select '123666'
    go
    --执行
    select max(a1) as a1,a2 from t1,t2 where a2 like a1 +'%' group by a2
    ----------------------
    --输出结果
    a1                   a2                   
    -------------------- -------------------- 
    12                   122456
    123                  123456
    123                  123666
      

  6.   

    dawugui(潇洒老乌龟)大大,你的my_len是什么啊?
      

  7.   

    sp4大大,我在vf中写
    locate all for a2=max(a1)+'%'group by a2
    不成功,说什么参数不够啊.
      

  8.   

    SET ANSI OFF
    SET EXACT OFF
    SELECT 字段A,SPACE(20) AS 字段B INTO TABLE 目标表 FROM T1 ORDER BY 1
    SELECT 目标表
    SCAN
    SELECT TOP 1 字段A,LEN(ALLTRIM(字段A)) INTO CURSOR TEMP FROM T2 WHERE UPPER(ALLTRIM(字段A))=UPPER(ALLTRIM(目标表.字段A)) ORDER BY 2 DESC
    REPLACE 目标表.字段B WITH TEMP.字段A
    ENDSCAN
    BROWSE
      

  9.   

    shujubu() ( )
    dawugui(潇洒老乌龟)大大,你的my_len是什么啊?my_len,匹配的字符串的长度.