请教一个sql结果集查询的问题,谢谢

wyq29 2008-01-25 06:14:01
现在有两个表
[VIP]表:
id username stime etime(到期时间,日期类型)

[infor]表

id title content istuijian username sysdate

说明:
vip表有会员记录开始和到期日期 是时间类型
infor表 istuijian是vip会员发布推荐的信息(后台已经设定 最多只能推荐2条) "0"表示不推荐 系统默认; "1" 表示推荐
username是发布帐号,sysdate是自动插入记录的日期

现在查询结果集:

列举所有vip表中未到期的会员发布信息的id title sysdate,要求:如果有推荐(1条或2条都行)则显示推荐的信息,如果没有推荐信息则显示最新发布的2条信息。
...全文
358 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一品梅 2008-01-25
  • 打赏
  • 举报
回复
楼上的实在是高.
-狙击手- 2008-01-25
  • 打赏
  • 举报
回复
[VIP]表:
id username stime etime(到期时间,日期类型)

[infor]表

id title content istuijian username sysdate

--

select b.*
from (select *
from vip
where datediff(d,getdate(),etime)>=0) a
left join
(select top 2 *
from infor
order by istuijian desc,sysdate desc) b
on a.id = b.id
dawugui 2008-01-25
  • 打赏
  • 举报
回复
select t.* from (select a.id,a.title,a.sysdate from infor a , VIP b where a.id = b.id and a.istuijian = 1 and b.etime < getdate()) t
where sysdate in
(
select top 2 sysdate from (select a.id,a.title,a.sysdate from infor a , VIP b where a.id = b.id and a.istuijian = 1 and b.etime < getdate()) m where id=t.id order by sysdate desc
)
union all
select t.* from (select a.id,a.title,a.sysdate from infor a , VIP b where a.id = b.id and a.istuijian = 0 and b.etime < getdate()) t
where sysdate in
(
select top 2 sysdate from (select a.id,a.title,a.sysdate from infor a , VIP b where a.id = b.id and a.istuijian = 1 and b.etime < getdate()) m where id=t.id order by sysdate desc
)
中国风 2008-01-25
  • 打赏
  • 举报
回复
select 
b.*
from
[VIP] a
join
[infor] b on a.username=b.username
where
datediff(d,getdate(),a.etime)>=0
ID in(
select
top 2 ID
from [infor]
where username=b.username and istuijian=0
order by sysdate desc )

union all

select
b.*
from
[VIP] a
join
[infor] b on a.username=b.username and a.istuijian>0
where
datediff(d,getdate(),a.etime)>=0
一品梅 2008-01-25
  • 打赏
  • 举报
回复
学习.二楼少了一个有推荐信息的情况.
dawugui 2008-01-25
  • 打赏
  • 举报
回复
select t.* from (select a.id,a.title,a.sysdate from infor a , VIP b where a.id = b.id and b.etime < getdate()) t
where sysdate in
(
select top 2 sysdate from (select a.id,a.title,a.sysdate from infor a , VIP b where a.id = b.id and b.etime < getdate()) m where id=t.id order by sysdate desc
)
tim_spac 2008-01-25
  • 打赏
  • 举报
回复
-- 所有vip表中未到期的会员发布信息的id,title,sysdate
select id, title, sysdate into #t1 from info where 1=2

insert into #t1
select top 2 id, title, sysdate
from infor
where exists ( select 1 from vip
where etime>getdate() -- vip表中未到期的会员
and vip.username = infor.username )
and istuijian=1
order by sysdate
-- 有推荐(1条或2条都行)则显示推荐的信息

if @@rowcount=0 begin
-- 如果没有推荐信息则显示最新发布的2条信息。
insert into #t1
select top 2 id, title, sysdate
from infor
where exists ( select 1 from vip
where etime>getdate() -- vip表中未到期的会员
and vip.username = infor.username )
and istuijian=0
order by sysdate desc
end

select * from #t1
drop table #t1

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧