我有这样2个表,是一对多关系
a表
id name sex...
1  张三  男
2  李四  女b表
id userid task starttime   endtime     addtime...
1  1      任务1 2006-12-12 2006-12-12  2006-12-12
2  1      任务2 2006-12-13 2006-12-18  2006-12-13
3  2      任务4 2006-12-12 2006-12-17  2006-12-12我要把a表的数据全列出来,然后在右边加上与之关联的b表中最近一条数据
select a.*,
       (select top 1 task from b where b.userid=a.id order by id desc) as task ,
       (select top 1 starttime from b where b.userid=a.id order by id desc) as starttime 
from a这样可以达到我的要求,但感觉不好,如果b表中有n多个字段,我这个查询要全列出来,那语句中要包含n个(select...),所以求更好的办法???

解决方案 »

  1.   

    select * from a inner join b on a.id=b.id
      

  2.   

    --这样?
    select a.* ,b.*
    from a 
    left join 
    (select * from b t 
    where not exists (select 1 from b where userid =t.userid and id >b.id)
    ) m on a.id =m.id 
      

  3.   

    --不要用*号
    select a.id,a.name,a.sex,b.task,b.starttime,...
    from a,(select * from b bb where no exists (select 1 from b where userid = bb.userid and id > bb.id))b
    where a.id = b.userid
      

  4.   

    换个思路阿,楼主先把b表中的数据,按userid分类取出id为最大的那一条记录,在联合 a 表做查询阿.
      

  5.   

    coolingpipe(冷箫轻笛) ( ) 信誉:100    Blog  2006-12-13 15:48:38  得分: 0     --不要用*号
    ------
     含n个(select...),-----
    楼主说 n 个我才用的
      

  6.   

    楼上的,不行,
    如果a表中某记录在b表中没有与之对应的数据,那a表中该记录不会列出来,我的目的是想要它列出来
      

  7.   

    不是说你,xiaoku,呵呵,说楼主呢还有你写的查询有点笔误,嘿嘿
      

  8.   

    呵呵,这么多回复呀,谢谢大家
    我上面说的楼主是:crazyflower
      

  9.   

    net205(培养一批有理想有道德有文化有纪律的新时代四有小姐) ( ) 信誉:100    Blog  2006-12-13 15:50:29  得分: 0  
      
       楼上的,不行,
    如果a表中某记录在b表中没有与之对应的数据,那a表中该记录不会列出来,我的目的是想要它列出来
      =================================================哦,还有这种情况阿select a.id,a.name,a.sex,b.task,b.starttime,...
    from a left join (select * from b bb where no exists (select 1 from b where userid = bb.userid and id > bb.id))b
    on a.id = b.userid 
      

  10.   

    如果a表中某记录在b表中没有与之对应的数据,那a表中该记录不会列出来,我的目的是想要它列出来===========================================这是典型的用外联接的例子
      

  11.   

    select * from a join b on a.id=b.id where id in(select 1 id from b b where userid=b.userid and addtime>b.addtime)
      

  12.   

    我那里有了?哦知道了
    select a.* ,b.*
    from a 
    left join 
    (select * from b t 
    where not exists (select 1 from b where userid =t.userid and id >b.id)
    ) m on a.id =m.userid   --烧了userid
      

  13.   


    create table A(id int,  name nvarchar(10),  sex nvarchar(10))
    insert A select 1,  '张三',  '男'
    union all select 2,  '李四',  '女'create table B(id int, userid int, task nvarchar(10), starttime datetime, endtime datetime,  addtime datetime)
    insert B select 1,  1,      '任务1', '2006-12-12', '2006-12-12',  '2006-12-12'
    union all select 2,  1,      '任务2', '2006-12-13', '2006-12-18',  '2006-12-13'
    union all select 3,  2,      '任务4', '2006-12-12', '2006-12-17',  '2006-12-12'select * from A
    inner join B on A.id=B.userid
    where B.id in(
    select max(id) from B group by userid)--result
    id          name       sex        id          userid      task       starttime                                              endtime                                                addtime                                                
    ----------- ---------- ---------- ----------- ----------- ---------- ------------------------------------------------------ ------------------------------------------------------ ------------------------------------------------------ 
    1           张三         男          2           1           任务2        2006-12-13 00:00:00.000                                2006-12-18 00:00:00.000                                2006-12-13 00:00:00.000
    2           李四         女          3           2           任务4        2006-12-12 00:00:00.000                                2006-12-17 00:00:00.000                                2006-12-12 00:00:00.000(2 row(s) affected)
      

  14.   

    谢谢marco08(天道酬勤) 写了这么多.
    你的存在问题,跟crazyflower的一样,如果b表中没有"任务4",则"李四"不会列出来,不符合我的要求目前coolingpipe(冷箫轻笛)的回复符合我的要求:
    ------
    哦,还有这种情况阿select a.id,a.name,a.sex,b.task,b.starttime,...
    from a left join (select * from b bb where no exists (select 1 from b where userid = bb.userid and id > bb.id))b
    on a.id = b.userid
      

  15.   

    coolingpipe(冷箫轻笛)写的错把not写成no了,上面用*代替也可以的呀
      

  16.   


    --oo ,不好意思,自己的错误太多了
    select a.* ,m.* --这里又一个
    from a 
    left join 
    (select * from b t 
    where not exists (select 1 from b where userid =t.userid and id> t.id) --这里一个
    ) m on a.id =m.userid   --烧了userid
    id          name       sex        id          userid      task       starttime                                              endtime                                                addtime                                                
    ----------- ---------- ---------- ----------- ----------- ---------- ------------------------------------------------------ ------------------------------------------------------ ------------------------------------------------------ 
    1           张三         男          2           1           任务2        2006-12-13 00:00:00.000                                2006-12-18 00:00:00.000                                2006-12-13 00:00:00.000
    2           李四         女          3           2           任务4        2006-12-12 00:00:00.000                                2006-12-17 00:00:00.000                                2006-12-12 00:00:00.000(所影响的行数为 2 行)
      

  17.   

    呵呵,xiaoku(野蛮人(^v^)) 不好意思
    你的我试过了,我这帖子只是举了个例子,
    然后按这个例子测试,发现有问题,当时就知道把b.*改成m.*
    然后测试出"张三"有2条,后来改成我实际的2个表,后来自己搞晕了,没换好,所以没试出来刚才又试了下。通过.
    改了你上面说的t.id
    caixia615的,没明白意思,没试成功再次谢谢各位
      

  18.   

    呵呵
    xiaoku(野蛮人(^v^)) 
    你仔细看看,其实你和coolingpipe(冷箫轻笛)2个的一样,他用的是not exists