从3个表查询中选择数据

时间:2014-04-02 06:49:17

标签: sql

我的查询如下......

select * 
from UserMaster um 
inner join  ForumMaster fm on um.UserID = fm.UserID 
left join  
    (select * from CommentMaster cm) cmt on cmt.TopicID = @TopicID

我的表格如下......

  • UserMasterUserID, FName, LName
  • CommentMasterCommentID, TopicID, Comment
  • ForumMasterTopicID, UserID, Topic, DateCreated

我希望上面三张表中的数据具有相同的TopicID ...

2 个答案:

答案 0 :(得分:0)

select * 
from UserMaster um 
    left outer join  ForumMaster fm on um.UserID = fm.UserID 
    left outer join  CommentMaster cm on cm.TopicId = fm.TopicId
where 
    cm.TopicId = @TopicId

答案 1 :(得分:0)

试试这个

SELECT * FROM UserMaster um, CommentMaster cm, ForumMaster fm WHERE fm.UserID = um.UserID AND fm.TopicID = cm.TopicID

我在不使用join命令的情况下加入表。