我想问下在SQL查询里有没有这样的查询,在一个表里按自己定义的顺序,比如表一
fruit order
apple 1
banana 2
peach 3
tomato 4
我要查询里按照表一里的顺序排列表二
表二是这样
姓名 喜欢的水果
李明 banana
张三 apple
小王 peach
三毛 apple排序后为
张三 apple
三毛 apple
李明 banana
小王 peach
fruit order
apple 1
banana 2
peach 3
tomato 4
我要查询里按照表一里的顺序排列表二
表二是这样
姓名 喜欢的水果
李明 banana
张三 apple
小王 peach
三毛 apple排序后为
张三 apple
三毛 apple
李明 banana
小王 peach
order by a.order
from 表二 left join 表1 on 表二.喜欢的水果 = 表一.fruit
order by 表一.order;
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