日期:2014-05-18  浏览次数:20547 次

求一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