加入两个select语句

时间:2017-02-02 21:19:44

标签: sql sql-server

有人可以,就如何加入以下两个选择陈述给我一些建议:

SELECT TOP 200 * 
FROM dbo.Creation_LimitsAndExclusions WITH (nolock) 
WHERE LeagueCodeID = 37

select * 
from dbo.Creation_Markets with (nolock) 
where ClassID = 9 and IsParentMatch = 1

提前致谢。

2 个答案:

答案 0 :(得分:1)

我不知道密钥是什么,但如果密钥在两个表中都是CreationID,那么连接将如下所示:

SELECT TOP 200 * 
FROM dbo.Creation_LimitsAndExclusions WITH (nolock) 
LEFT JOIN dbo.Creation_Markets ON Creation_LimitsAndExclusions.CreationID = Creation_Markets.CreationID 
WHERE Creation_LimitsAndExclusions.LeagueCodeID = 37
  AND Creation_Markets.ClassID = 9 and Creation_Markets.IsParentMatch = 1

答案 1 :(得分:0)

也不知道密钥是什么,但我会在where子句中添加cm.creationId IS NOT NULL条件,以确保我们不从Creation_LimitsAndExclusions中获取不匹配的行

SELECT TOP 200 * 
FROM dbo.Creation_LimitsAndExclusions cle WITH (nolock) 
LEFT JOIN dbo.Creation_Markets cm
    ON cle.CreationID = cm.CreationID 
WHERE cle.LeagueCodeID = 37
    AND cm.ClassID = 9 
    AND cm.IsParentMatch = 1
    AND cm.creationID IS NOT NULL