是这样的,我查询一个表,要求按某个字段在另外一个表中的另外一个字段排列
比如
表1中A字段有  A1,B1,C1等,
表2中A字段有  A,B,C等,B字段对应是2,0,1等
最后我要求表1的排列结果为 B1,C1,A1
即按表1中A字段的第一个字母在表2中的对应位置的B字段的值排列

解决方案 »

  1.   

    select * from 表1
    order by case when A=A1 then 2 when A=b1 then 0 when A=c1 then 1 end
      

  2.   

    select a.* 
    from 表1 a join 表2 b on  left(a.a1,1)=b.a
    order by b.b
      

  3.   

    --========+++++++++++++++++++++++++++++++++++==========
    --======= 每天都在进步,却依然追不上地球的自传=========
    --======= By: zc_0101 At:2009-08-18 22:00:05=========
    --========++++++++++++++++++++++++++++++++++++=========
    --> 测试数据: #a
    if object_id('tempdb.dbo.#a') is not null drop table #a
    create table #a (A varchar(2))
    insert into #a
    select 'A1' union all
    select 'B1' union all
    select 'C1'
    GO
    --> 测试数据: #b
    if object_id('tempdb.dbo.#b') is not null drop table #b
    create table #b (A varchar(2),B int)
    insert into #b
    select 'A1',2 union all
    select 'B1',0 union all
    select 'C1',1
     ----------------查询------------
    SELECT #a.A from #a inner join #b on #a.A=#b.A order by #b.B
    ----------------结果--------------
    /*
    B1
    C1
    A1
    */
      

  4.   

    select a.* 
    from 表1 a join 表2 b on  left(a.a,1)=b.a
    order by b.b
      

  5.   

    数据就是表1的A字段有A1,B1,C1三个数据,
    表2中A字段有  A,B,C三个数据,B字段对应是2,0,1三个数据啊
    最后得到结果应为
    B1,C1,A1 
      

  6.   

    SELECT ID=IDENTITY(INT,1,1),A INTO #T FROM TB1DECLARE @VAR VARCARH(50)
    SELECT @VAR=ISNULL(@VAR+',','')+LTRIM(B) FROM TB2SELECT A FROM #T ORDER BY CHARINDEX(LTRIM(ID),@VAR)???
      

  7.   


    --> 测试数据:[TA]
    if object_id('[TA]') is not null drop table [TA]
    create table [TA]([A] varchar(2))
    insert [TA]
    select 'A1' union all
    select 'B1' union all
    select 'C1'
    --> 测试数据:[TB]
    if object_id('[TB]') is not null drop table [TB]
    create table [TB]([A] varchar(1),[B] int)
    insert [TB]
    select 'A',2 union all
    select 'B',0 union all
    select 'C',1select * from TA t order by (select B from TB where left(T.A,1)=A)
    /*
    A
    ----
    B1
    C1
    A1(3 行受影响)*/
    drop table TA,TB
      

  8.   


    declare @a table(A varchar(20))
    insert @a select 
    'A1' union all select
    'B1' union all select
    'C1' declare @B table(A varchar(20),B INT)
    insert @B select 
    'A',2 union all select
    'B',0 union all select
    'C',1 
    SELECT T1.A FROM @A T1,@B T2
    WHERE LEFT(T1.A,1)=T2.A
    ORDER BY T2.BA
    --------------------
    B1
    C1
    A1(3 行受影响)
      

  9.   

    select 

    from 
    表1 
    order by 
      case 
      when A=A1 then 2 
      when A=B1 then 0 
      when A=C1 then 1 end