现在想显示Big表里面的记录,要求包含Small表的记录排前面,
请问用sql语句是否能实现?
比如:
Big记录
testId
1
2
3
4
5
6
7
8
9
..........Small记录
testId
5
6
7现在要显示成如下的排序:
Big记录
testId
5
6
7
1
2
3
4
8
9
..........

解决方案 »

  1.   

    select testId from Small
    union all 
    select testId from Big
      

  2.   

    --如果要去掉重複記錄, 則用以下SQL
    select testId from Small
    union 
    select testId from Big
      

  3.   

    楼上错.(就目前数据是对的,如果SMALL里面多个数据如10就不行了.)select testId from big where testid in (select testid from small)
    union all
    select testId from big where testid not in (select testid from small)
      

  4.   

    declare @b table(id int)
    insert @b select 1
    union all select 2
    union all select 3
    union all select 4
    union all select 5
    union all select 6
    union all select 7
    union all select 8
    union all select 9declare @s table(id int)
    insert @s select 5
    union all select 6
    union all select 7select b.* from @b b
    left join @s s
    on s.id=b.id
    order by isnull(s.id,1000000),b.id
      

  5.   

    用union的话可以这样
    select *,frm='1' from @s s
    union
    select *,frm='2'  from @b b where not exists(select 1 from @s s where s.id=b.id)
    order by frm,id
      

  6.   

    Small没有重复的记录,是big的一个子集,每个表的TestId字段不重复
      

  7.   

    Small没有重复的记录,是big的一个子集,每个表的TestId字段不重复
    那就不会有个数据在big里面没有的情况.select testId from Small
    union all 
    select testId from Big where testid not in (select testid from small)或select testId from big where testid in (select testid from small)
    union all 
    select testId from Big where testid not in (select testid from small)