union all使用之怪现象,请高手解答,在线等!

来源:百度知道 编辑:UC知道 时间:2024/05/26 15:56:02
想实现如下功能:会员信息总是放在最前面(按时间排序),当会员信息全部输出时才输出非会员信息(按时间排序),用union all实现时有如下怪现象,请高手解答,
(表名:gongying,字段名:g_title(标题),g_image(图像),g_date(日期),ifvip(会员识别符号))

sql="select * from (select g_title,g,image,g_date from gongying where ifvip=1 order by g_date desc
union all
select g_title,g,image,g_date from gongying where ifvip=0 order by g_date desc) "
调试能输出会员信息\非会员信息,但会员信息与非会员信息交替,无实现会员信息在前的功能!

sql="select * from (select g_title,g,image,g_date from gongying where ifvip=1 order by g_date desc) as A
union all
select * from (select g_title,g,image,g_date from gongying where ifvip=0 order by g_date desc) as B"

调试能输出会员信息\非会员信息,且实现会员信息在前的功能!但会员信息内部排序无按g_date desc来排序,非会员信息内部也没有按g_date desc排序!

如何才能实现???

是sql server 2000吗?
你这样的语句没必要用union all吧,你根据ifvip分组,然后再按g_date排序不就可以了?

select g_title,g,image,g_date from gongying group by ifvip order by g_date desc