读书人

求一sql语句用哦解决方案

发布时间: 2012-03-16 16:34:56 作者: rapoo

求一sql语句,急用哦
表1:hot,包括字段(id,tid)
表2:xxb,包括(id,tid,xm,xb,nl,bz)

在xxb找出xxb.tid=hot.tid前5条不重复信息,假如不足5条,则把bz=1的凑成5条


[解决办法]
SELECT distinct top 5 a.* FROM xxb as a INNER JOIN hot as b ON a.tid = b.tid
[解决办法]
select top 5 * from (
select distinct xxb.*,flag = 1 from hot,xxb
where hot.tid = xxb.tid
union
select distinct xxb.*,flag = 2 from xxb
where bz = 1) a
order by flag,id
[解决办法]
似乎排序应指定一下:
order by flag asc ,id desc

读书人网 >SQL Server

热点推荐