select * form Table1 as a left join Table2 as b order by b.time,a.displayorde,a.id DESC
create table Table1 ( tid int, displayOrder int, yyyy varchar(10) ) insert into Table1 select 1,0,'0' union all select 2 , 3 , 'xxx' union all select 3 , 0 , '0' union all select 4 , 0 , '0' union all select 5 , 2 , 'www' union all select 6 , 0 , '0' union all select 7 , 0 , 'www' create table Table2 ( tid int, time varchar(10) ) insert into Table2 select 2,'time2' union all select 5,'time3' union all select 7,'time4'select t1.tid from Table1 t1 left join Table2 t2 on t1.tid = t2.tid order by t2.time desc,t1.displayOrder drop table Table1,Table2 /* tid ----------- 7 5 2 1 3 4 6(所影响的行数为 7 行)*/
(
tid int,
displayOrder int,
yyyy varchar(10)
)
insert into Table1 select 1,0,'0'
union all select 2 , 3 , 'xxx'
union all select 3 , 0 , '0'
union all select 4 , 0 , '0'
union all select 5 , 2 , 'www'
union all select 6 , 0 , '0'
union all select 7 , 0 , 'www'
create table Table2
(
tid int,
time varchar(10)
)
insert into Table2 select 2,'time2'
union all select 5,'time3'
union all select 7,'time4'select t1.tid from Table1 t1 left join Table2 t2 on t1.tid = t2.tid order by t2.time desc,t1.displayOrder
drop table Table1,Table2
/*
tid
-----------
7
5
2
1
3
4
6(所影响的行数为 7 行)*/