日期:2014-05-20  浏览次数:20610 次

请问 一个 视图 的建立 ? 新手
UserInfo(UserId,UserName,HeadImage)   用户表

UserComment(CommentId,UserId,CommentContent,publishTime)   评论表

用户表   一对多   评论表

现在   要取出   每个用户的   最新的一条评论   .

视图(UserId,UserName,HeadImage,CommentId,CommentContent,PublishTime)


怎么建立阿?


------解决方案--------------------
你说的是什么视图?是SQL语句吗
select top 1 usr.UserID, usr.UserName, usr.HeadImage, cmt.CommentID, cmt.CommentContent, cmt.PublishTime
from UserInfo as usr, UserComment as cmt
where usr.UserID=cmt.UserID
order by cmt.PublishTime desc
------解决方案--------------------
Create View V_RandQuestion
As
Select UC.UserID, UserName, HeadImage, CommentId, CommentContent, publishTime From UserComment UC Left Join UserInfo UI On UC.UserID = UI.UserID

------解决方案--------------------
select a.UserID, a.UserName, a.HeadImage, b.CommentID, b.CommentContent, b.PublishTime
from UserInfo as a inner join
UserComment as b
on
a.UserID=b.UserID inner join
(select UserID ,max(PublishTIme) as PublishTIme group by UserID ) c
on
b.UserID =c.UserID and b.PublishTime=c.PublishTime

------解决方案--------------------
在sqlserver下,Snowdust(雪尘) 正解。
如果在access下,则建立一个查询,把select语句写入,存储即可
------解决方案--------------------
mark