有三个表:
1、Web表,字段:ID,WebName
2、Channel表,字段:ID,WebID,ChannelName
2、Content表,字段:ID,ChannelID,Title,Content,CreateDate
其中表Channel.WebID对应Web表的ID,Content.ChannelID对应表的Channel.ID
如何用一个语句取得每个Channel.ChannelName所对应的Content的最新一条记录,得到的列表如下:
Content.ID,WebName,ChannelName,Title,Content,CreateDate例如:
表Web
1,网易
表Channel
1,1,新闻
2,1,女性
3,1,数码
表Content
1,1,中国法定节假日调整内容公布,中国法定节假日调整内容公布....,2007-11-8
2,1,长虹手机爆炸火烧鸭棚,长虹手机爆炸火烧鸭棚...,2007-11-9
3,3,索尼爱立信W890/K660/W380三新机发布,索尼爱立信W890/K660/W380三新机发布...,2007-11-6得到的结果如下:
1,网易,新闻,长虹手机爆炸火烧鸭棚,长虹手机爆炸火烧鸭棚...,2007-11-9
3,网易,数码,索尼爱立信W890/K660/W380三新机发布,索尼爱立信W890/K660/W380三新机发布...,2007-11-6
1、Web表,字段:ID,WebName
2、Channel表,字段:ID,WebID,ChannelName
2、Content表,字段:ID,ChannelID,Title,Content,CreateDate
其中表Channel.WebID对应Web表的ID,Content.ChannelID对应表的Channel.ID
如何用一个语句取得每个Channel.ChannelName所对应的Content的最新一条记录,得到的列表如下:
Content.ID,WebName,ChannelName,Title,Content,CreateDate例如:
表Web
1,网易
表Channel
1,1,新闻
2,1,女性
3,1,数码
表Content
1,1,中国法定节假日调整内容公布,中国法定节假日调整内容公布....,2007-11-8
2,1,长虹手机爆炸火烧鸭棚,长虹手机爆炸火烧鸭棚...,2007-11-9
3,3,索尼爱立信W890/K660/W380三新机发布,索尼爱立信W890/K660/W380三新机发布...,2007-11-6得到的结果如下:
1,网易,新闻,长虹手机爆炸火烧鸭棚,长虹手机爆炸火烧鸭棚...,2007-11-9
3,网易,数码,索尼爱立信W890/K660/W380三新机发布,索尼爱立信W890/K660/W380三新机发布...,2007-11-6
from Web a Inner Join Channel b On a.ID=b.WebID
Inner Join (select * from Content s where not exists(select 1 from Content where ChannelID=s.ChannelID and CreateDate>s.CreateDate))aa On b.Id=aa.ChannelID
declare @ChannelName varchar(32)
set @ChannelName='新闻'
select Con.ID, W.WebName, Chl.ChannelName, Con.Title, Con.Content, Con.CreateDate
from Channel Chl join Web W on Chl.WebID=W.ID
join Con on Chl.ID=Con.ChannelID
where Chl.ChannelName = @ChannelName
and Con.CreateDate=(select max(CreateDate) from Content where ID=Con.ID)
select Con.ID, W.WebName, Chl.ChannelName, Con.Title, Con.Content, Con.CreateDate
from Channel Chl join Web W on Chl.WebID=W.ID
join Con on Chl.ID=Con.ChannelID
where Con.CreateDate=(select max(CreateDate) from Content where ID=Con.ID)
inner join
(select top 1 date .. where classid=a.classid order by date desc) b
on a.date=b.date... from a
inner join
(select max(date) d from ... where classid=a.classid) b
on a.date=b.detc.
懒得敲了