我的一个表中有字段prdno(料号),它的值是像i0429-1。我的另一个表也有一个prdno字段其值为i0429,没有前一个表的-1,其中-1为号头。我现在想做的事想让这两张表关联,做成类似左外连接的样子,像table.prdno left outer join table2.prdno,但是因为这两个字段的内容只是部分相等,我不知道on后面放什么连接条件,也不知道能不能用'%like%'这样子的,希望各位帮帮忙

解决方案 »

  1.   

    table1 left join table2 on table1.prdno like table2.prdno+'-%'
      

  2.   


    select * from tb1 a
    left join tb2 b on charindex(b.prdno,a.prdno)>0
      

  3.   

    select * from tb1 a
    left join tb2 b on left(a.prdno,len(b.prdno)) = b.prdno以上几种都可以,添一种方法~和like效率一样
      

  4.   

    可以用like作为连接条件,也可以用不等符作为连接条件,northwind数据库里就有。
    你的连接条件可以写成
    left(a.pro,..)=b.por...
      

  5.   

    如果prdno 字段有索引,一楼的效果会更好.
      

  6.   

    select * from tb1 a left join tb2 b on charindex(b.prdno,a.prdno)>0
      

  7.   

    补充一下,我用一楼的试了一下,结果是可以的,可是查询结果是这样的
    I0429-1                         2 I0429 2
    I0429-1-XK                2 I0429 2
    I0429-1-Y                 18 I0429 2
    我想让I0429的值在这三行中只显示一次,怎么办,也就是下面这种效果
    I0429-1                   2 I0429 2
    I0429-1-XK                2 I0429
    I0429-1-Y                 18 I0429
      

  8.   


    select *,case when value1 = 1 then value1 else null end values1 
    from (
       select *,ROW_NUMBER() OVER(ORDER BY 值列 DESC) value1  from tb1 a left join tb2 b on charindex(b.prdno,a.prdno)>0
       ) as b
      

  9.   

    declare @tb1 table (id int null,prdno nvarchar(20) null)
    insert @tb1 values (1,'i0429-1')
    insert @tb1 values (2,'i0428-2')
    insert @tb1 values (3,'i0421-2')
    insert @tb1 values (4,'i0430-1')declare @tb2 table (id int null,prdno nvarchar(20) null,value int null)
    insert @tb2 values (1,'i0429',2)
    insert @tb2 values (2,'i0421',2)
    insert @tb2 values (3,'i0428',2)
    insert @tb2 values (4,'i0430',2)--select * from @tb1
    --select * from @tb2
    select * from @tb1 a left join @tb2 b on charindex(b.prdno,a.prdno)>0--select a.id id1,b.id id2,a.prdno prdno1,b.prdno prdno2,ROW_NUMBER() OVER(ORDER BY value DESC) value1  from @tb1 a left join @tb2 b on charindex(b.prdno,a.prdno)>0select *,case when value1 = 1 then value else null end values1 
    from (
        select a.id id1,b.id id2,a.prdno prdno1,b.prdno prdno2,b.value,ROW_NUMBER() OVER(ORDER BY value DESC) value1  from @tb1 a left join @tb2 b on charindex(b.prdno,a.prdno)>0
       ) d
    -----------------------id prdno id prdno value
    --1 i0429-1 1 i0429 2
    --2 i0428-2 3 i0428 2
    --3 i0421-2 2 i0421 2
    --4 i0430-1 4 i0430 2----------------------
    --id1 id2 prdno1 prdno2 value value1 values1
    --1 1 i0429-1 i0429 2 1 2
    --2 3 i0428-2 i0428 2 2 NULL
    --3 2 i0421-2 i0421 2 3 NULL
    --4 4 i0430-1 i0430 2 4 NULL
      

  10.   

    select *,case when value1 = 1 then value else null end values1 
    from (
        select a.id id1,b.id id2,a.prdno prdno1,b.prdno prdno2,b.value,ROW_NUMBER() OVER(PARTITION by b.prdno ORDER BY value DESC) value1  from @tb1 a left join @tb2 b on charindex(b.prdno,a.prdno)>0
       ) d--------------------
    --id1 id2 prdno1 prdno2 value value1 values1
    --3 2 i0421-2 i0421 2 1 2
    --2 3 i0428-2 i0428 2 1 2
    --1 1 i0429-1 i0429 2 1 2
    --1 5 i0429-1 i0429 2 2 NULL
    --4 4 i0430-1 i0430 2 1 2