select top 20 id, username as title,
shijian as joindate, 3 as ctype
from house.house.house_hz
order by id desc
union
select top 20 id, house_name as title,
shijian as joindate, 1 as ctype
from house.dbo.house_esf where type=0
order by id desc
union
select top 20 id, house_name as title,
shijian as joindate, 1 as ctype
from house.dbo.house_esf where type=1
order by id desc出错了,如何解决?

解决方案 »

  1.   

    select top 20 id, username as title,
    shijian as joindate, 3 as ctype
    from house.house.house_hz
    union
    select top 20 id, house_name as title,
    shijian as joindate, 1 as ctype
    from house.dbo.house_esf where type=0
    union
    select top 20 id, house_name as title,
    shijian as joindate, 1 as ctype
    from house.dbo.house_esf where type=1
    order by id desc
      

  2.   

    select * from (select top 20 id, username as title,
    shijian as joindate, 3 as ctype
    from house.house.house_hz
    order by id desc)a
    union
    select * from (select top 20 id, house_name as title,
    shijian as joindate, 1 as ctype
    from house.dbo.house_esf where type=0
    order by id desc)b
    union
    select * from (select top 20 id, house_name as title,
    shijian as joindate, 1 as ctype
    from house.dbo.house_esf where type=1
    order by id desc)c
      

  3.   

    Union 和 Union all 只能跟整个数据集的排序!!