我有这样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...),所以求更好的办法???
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...),所以求更好的办法???
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
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
------
含n个(select...),-----
楼主说 n 个我才用的
如果a表中某记录在b表中没有与之对应的数据,那a表中该记录不会列出来,我的目的是想要它列出来
我上面说的楼主是:crazyflower
楼上的,不行,
如果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
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
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)
你的存在问题,跟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
--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 行)
你的我试过了,我这帖子只是举了个例子,
然后按这个例子测试,发现有问题,当时就知道把b.*改成m.*
然后测试出"张三"有2条,后来改成我实际的2个表,后来自己搞晕了,没换好,所以没试出来刚才又试了下。通过.
改了你上面说的t.id
caixia615的,没明白意思,没试成功再次谢谢各位
xiaoku(野蛮人(^v^))
你仔细看看,其实你和coolingpipe(冷箫轻笛)2个的一样,他用的是not exists