select 
  a.c1,
  a.c2,
  a.c3,
  b.c2
from 
  tb a,
  tb b
where 
  a.c3=b.c1

解决方案 »

  1.   

    if object_id('tb') is not null drop table tb 
     go 
    create table tb([c1] varchar(10),[c2] varchar(10),[c3] varchar(10))
    insert tb select 'a','aa','b'
    union all select 'b','bb','c'
    union all select 'c','cc','e'
    union all select 'd','dd','a'
    union all select 'e','ee','a'
    goselect *,c4=(select c2 from tb where c1=t.c3) from tb t
    /*
    c1         c2         c3         c4
    ---------- ---------- ---------- ----------
    a          aa         b          bb
    b          bb         c          cc
    c          cc         e          ee
    d          dd         a          aa
    e          ee         a          aa(5 行受影响)
    */
      

  2.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([c1] varchar(1),[c2] varchar(2),[c3] varchar(1))
    insert [tb]
    select 'a','aa','b' union all
    select 'b','bb','c' union all
    select 'c','cc','e' union all
    select 'd','dd','a' union all
    select 'e','ee','a'
     
    ---查询---
    select 
      a.c1,
      a.c2,
      a.c3,
      b.c2
    from 
      tb a,
      tb b
    where 
      a.c3=b.c1---结果---
    c1   c2   c3   c2   
    ---- ---- ---- ---- 
    a    aa   b    bb
    b    bb   c    cc
    c    cc   e    ee
    d    dd   a    aa
    e    ee   a    aa(所影响的行数为 5 行)
      

  3.   

    if object_id('tb') is not null drop table tb 
     go 
    create table tb([c1] varchar(10),[c2] varchar(10),[c3] varchar(10))
    insert tb select 'a','aa','b'
    union all select 'b','bb','c'
    union all select 'c','cc','e'
    union all select 'd','dd','a'
    union all select 'e','ee','a'
    go
    select *,(select [c2] from tb where [c1]=t.c3) c4 from tb t
      

  4.   

    if object_id('tb') is not null drop table tb 
     go 
    create table tb([c1] varchar(10),[c2] varchar(10),[c3] varchar(10))
    insert tb select 'a','aa','b'
    union all select 'b','bb','c'
    union all select 'c','cc','e'
    union all select 'd','dd','a'
    union all select 'e','ee','a'
    go
    select *,(select [c2] from tb where [c1]=t.c3) c4 from tb t
    c1         c2         c3         c4
    ---------- ---------- ---------- ----------
    a          aa         b          bb
    b          bb         c          cc
    c          cc         e          ee
    d          dd         a          aa
    e          ee         a          aa(5 行受影响)
      

  5.   

    那就用表连接if object_id('tb') is not null drop table tb 
     go 
    create table tb([c1] varchar(10),[c2] varchar(10),[c3] varchar(10))
    insert tb select 'a','aa','b'
    union all select 'b','bb','c'
    union all select 'c','cc','e'
    union all select 'd','dd','a'
    union all select 'e','ee','a'
    go
    select a.c1,a.c2,a.c3, b.c2 from tb a join tb b
    on a.c3=b.c1c1         c2         c3         c4
    ---------- ---------- ---------- ----------
    a          aa         b          bb
    b          bb         c          cc
    c          cc         e          ee
    d          dd         a          aa
    e          ee         a          aa(5 行受影响)