我想问下在SQL查询里有没有这样的查询,在一个表里按自己定义的顺序,比如表一
fruit    order
apple    1
banana   2
peach    3
tomato   4
我要查询里按照表一里的顺序排列表二 
表二是这样
姓名  喜欢的水果
李明  banana
张三  apple
小王  peach
三毛  apple排序后为
张三  apple 
三毛  apple
李明  banana
小王  peach 

解决方案 »

  1.   

    select b.姓名,a.fruit from 表1 as b inner join 表2 as b on a.fruit=b.喜欢的水果
    order by a.order
      

  2.   

    select 姓名,喜欢的水果 from 表二 order by 喜欢的水果;
      

  3.   

    select 姓名,喜欢的水果 
    from 表二 left join 表1 on 表二.喜欢的水果 = 表一.fruit
    order by 表一.order;
      

  4.   

    create table t1(fruit varchar(50),   order int )
    gocreate table t2(姓名 varchar(50)  喜欢的水果 varchar(50))
    go--插入T1
    insert into t1(fruit,order)
    select 'apple' ,   1
    union all
    select 'banana' ,  2
    union all
    select 'peach'  ,  3
    union all
    select 'tomato',   4
    --插入T2
    insert into t2(姓名,喜欢的水果)
    select '李明' ,'apple'
    union all
    select '张三','banana'
    union all
    select '小王','peach' 
    union all
    select '三毛' ,'tomato'  
    go查询的句子
    select t2.姓名,t2.喜欢的水果
    from t1 a,t2 b
    where a.fruit = b.喜欢的水果
    order by a.order