首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如果按照另一个表排序?解决方法

2012-05-11 
如果按照另一个表排序?SQL codecreate table table1(a varchar(20))create table table2(b varchar(20))

如果按照另一个表排序?

SQL code
create table table1(a varchar(20));create table table2(b varchar(20));insert into table1 values('aa');insert into table2 values('vv');insert into table2 values('ff');insert into table2 values('baab');insert into table2 values('bab');select a from table1select b from table2drop table table1drop table table2


有两个表,table2如何按照table1排序呢, 也就是说table2的值包含table1的值拍在最前面, 只返回table2的数据 table2中 baab在最前面

[解决办法]
SQL code
select b.*  from table2 b left join table1 a on charindex(a.a,b.b)>0order by   case when a.a is not null then 1 else 2 end/**b--------------------baabbabvvff(4 行受影响)**/
[解决办法]
SQL code
select b from table2 order by (select count(1) from table1 where charindex(a,b)>0) desc/*b--------------------baabbabvvff*/
[解决办法]
SQL code
create table table1(a varchar(20));create table table2(b varchar(20));insert into table1 values('aa');insert into table2 values('vv');insert into table2 values('ff');insert into table2 values('baab');insert into table2 values('bab');select * from table2order by CHARINDEX((select a from table1),b) desc/*b------------------------baabbabvvff*/ 

热点排行