问你们 个问题:
表a     列1               表b  列2
        a                      b
        c                      aa
        b                      aaa
形成表C
      列1    列2
      a     aa
      a     aaa
      c     <null>
就是说从表1 中匹配与表2 中相似的值,a 与 aa 相似形成一记录。a与aaa相似生成一记录. b与b 相同不能生成 。 
怎么写SQL 语句

解决方案 »

  1.   

    select *
    from a,b
    where a.列1<>b.列2
    and b.列2 like rtrim(a.列1)+'%'
      

  2.   

    or:select *
    from a,b
    where a.列1<>b.列2
    and left(b.列2,len(a.列1))=a.列1
      

  3.   

    以上都错了,看看一下测试:declare @a table (
    列1 varchar(10)
    )
    insert @a
    select 'a'
    union all select 'c'
    union all select 'b'declare @b table (
    列2 varchar(10)
    )
    insert @b
    select 'b'
    union all select 'aa'
    union all select 'aaa'--1
    select *
    from @a a left join @b b
    on  b.列2 like rtrim(a.列1)+'%'
    where b.列2 is null or a.列1<>b.列2--2
    select *
    from @a a left join @b b
    on  left(b.列2,len(a.列1))=a.列1
    where b.列2 is null or a.列1<>b.列2
      

  4.   

    declare @t table(f1 varchar(10))
    declare @x table(f2 varchar(10))
    insert @t select 'a' union all select 'c' union all select 'b'
    insert @x select 'b' union all select 'aa' union all select 'aaa'
    select f1,f2 from @t a
    full join @x b
    on f2 like f1+'[a-z]%'
    where not exists(select 1 from @x c where f2=a.f1) and f1  is not null/*
    f1 f2
    a aa
    a aaa
    c NULL
    */