a表 
aid atitle
m表
mid mtitle
b表
bid  aid(a表的主键) mid(m表主键)  btitle  btime hid现在想读出10条数据,要求这前十条数据中有atitle,mid,mtitle,bid,btitle,btime,hid,并且还要满足下面条件1 . aid不一样2 . 相同aid的数据,只显示一条,并且这条数据中的 btime 是大于当前时间,并离当前时间最近的一条3 . 不足10条的,aid相同的可以重复出现多条4 . hid=9 or  hid=14还需要补充的事情是,这里读出的bid是,当前时间btime所在的这条数据的bid,请问这个SQL语句该怎么写呢?

解决方案 »

  1.   

    a表
    aid  atitle
    1    开会
    2    吃饭
    3    休息
    ... m表
    mid  mtitle
    1    沈阳
    2    天津
    3    北京
    4    上海
    b表
    bid      aid(a表的主键)      mid(m表主键)      btitle            btime        hid 
    1         1                   1                沈阳计划会议      2009-2-10    9
    2         1                   2                北京计划会议      2009-2-10    8
    3         1                   1                沈阳计划会议      2009-3-10    14
    4         1                   1                沈阳计划会议      2009-5-10    14
    5         1                   1                沈阳计划会议      2009-6-10    14
    6         2                   4                上海会客吃饭      2009-4-10    6w我想读出一组数据,满足下面条件1 . aid不一样2 . 相同aid的数据,只显示一条,并且这条数据中的 btime 是大于当前时间,并离当前时间最近的一条3 . hid=9 or  hid=14 按今天是2009-03-25算,大于当前时间,排除了前三条,然后根据 hid=9 or hid=14 ,那么第6条就不满足,只有第4条和第5条,但相同aid的只能显示一条,那么 4和5只能显示一条,然后要求显示的是离当前时间最近的一条,最后得到的结果是 bid 为 4的这条  bid      aid(a表的主键)      mid(m表主键)      btitle            btime        hid 
    4         1                   1                沈阳计划会议      2009-5-10    14最后显示的结果是
    bid    atitle   mtitle  btitle              btime        hid
    4      开会     沈阳    沈阳计划会议        2009-5-10    14请问这个SQL语句怎么写呢?
      

  2.   

    select n.bid , a.atitle , m.mtitle , n.btitle , n.btime , n.hid from
    (select t.* from b t where btime > getdate() and (hid=9 or hid=14) and btime = (select min(btime) from b btime > getdate() and (hid=9 or hid=14) and aid = t.aid)) n,a,m
    where n.aid = a.aid and n.mid = m.mid 
      

  3.   

    create table a(aid int, atitle varchar(10))
    insert into a values(1 , '开会') 
    insert into a values(2 , '吃饭') 
    insert into a values(3 , '休息') 
    create table m(mid int, mtitle varchar(10))
    insert into m values(1 , '沈阳') 
    insert into m values(2 , '天津') 
    insert into m values(3 , '北京') 
    insert into m values(4 , '上海') 
    create table b(bid int, aid int, mid int, btitle varchar(20), btime datetime, hid int)
    insert into b values(1 , 1 , 1 , '沈阳计划会议' , '2009-2-10' , 9 )
    insert into b values(2 , 1 , 2 , '北京计划会议' , '2009-2-10' , 8 )
    insert into b values(3 , 1 , 1 , '沈阳计划会议' , '2009-3-10' , 14) 
    insert into b values(4 , 1 , 1 , '沈阳计划会议' , '2009-5-10' , 14) 
    insert into b values(5 , 1 , 1 , '沈阳计划会议' , '2009-6-10' , 14) 
    insert into b values(6 , 2 , 4 , '上海会客吃饭' , '2009-4-10' , 6 )
    goselect n.bid , a.atitle , m.mtitle , n.btitle , n.btime , n.hid from
    (select t.* from b t where btime > getdate() and (hid=9 or hid=14) and btime = (select min(btime) from b where btime > getdate() and (hid=9 or hid=14) and aid = t.aid)) n,a,m
    where n.aid = a.aid and n.mid = m.mid drop table a , b , m/*
    bid         atitle     mtitle     btitle               btime                                                  hid         
    ----------- ---------- ---------- -------------------- ------------------------------------------------------ ----------- 
    4           开会         沈阳         沈阳计划会议               2009-05-10 00:00:00.000                                14(所影响的行数为 1 行)*/
      

  4.   

    貌似还是不行,我上面是一个当数据量多的时候,还是会出现aid相同的数据存在
    比如在下面的语句中create table a(aid int, atitle varchar(10))
    insert into a values(1 , '开会') 
    insert into a values(2 , '吃饭') 
    insert into a values(3 , '休息') 
    create table m(mid int, mtitle varchar(10))
    insert into m values(1 , '沈阳') 
    insert into m values(2 , '天津') 
    insert into m values(3 , '北京') 
    insert into m values(4 , '上海') 
    create table b(bid int, aid int, mid int, btitle varchar(20), btime datetime, hid int)
    insert into b values(1 , 1 , 1 , '沈阳计划会议' , '2009-2-10' , 9 )
    insert into b values(2 , 1 , 2 , '北京计划会议' , '2009-2-10' , 8 )
    insert into b values(3 , 1 , 1 , '沈阳计划会议' , '2009-3-10' , 14) 
    insert into b values(4 , 1 , 1 , '沈阳计划会议' , '2009-5-10' , 14) 
    insert into b values(5 , 1 , 1 , '沈阳计划会议' , '2009-6-10' , 14) 
    insert into b values(6 , 2 , 4 , '上海会客吃饭' , '2009-4-10' , 6 )
    中,加入
    insert into b values(7 , 1 , 2 , '天津计划会议' , '2009-5-10' , 14)当按上面的select语句的时候,就会出现aid相同的情况bid         atitle     mtitle     btitle               btime                                                  hid         
    ----------- ---------- ---------- -------------------- ------------------------------------------------------ ----------- 
    4           开会         沈阳         沈阳计划会议               2009-05-10 00:00:00.000                      14
    7           开会         天津         天津计划会议               2009-05-10 00:00:00.000                      14两条aid数据了~如果我想显示一条,取bid小的显示该怎么处理呢?