如何转换以下内容以使用INNER JOIN而不是嵌套的SELECT?
SELECT
[Name].[NameValueID],
[Name].[NameTypeID],
[Name].[NameID],
[Name].[Value]
FROM [Name]
WHERE Name.NameTypeID IN ( SELECT NameTypeID FROM @tbNameType )
OR Name.NameID IN ( SELECT NameID FROM @tbName)
答案 0 :(得分:3)
这个很棘手,因为它是“OR”条件而不是“AND”。但我认为这样做会:
SELECT
[Name].[NameValueID],
[Name].[NameTypeID],
[Name].[NameID],
[Name].[Value]
FROM [Name]
INNER JOIN ( SELECT NameTypeID FROM @tbNameType ) t ON t.NameTypeID=Name.NameTypeID
UNION
SELECT
[Name].[NameValueID],
[Name].[NameTypeID],
[Name].[NameID],
[Name].[Value]
FROM [Name]
INNER JOIN ( SELECT NameID FROM @tbName) t ON t.NameID = Name.NameID
答案 1 :(得分:3)
SELECT DISTINCT
Name.NameValueID,
Name.NameTypeID,
Name.NameID,
Name.Value
FROM
Name
LEFT JOIN @tbNameType a ON a.NameTypeID=Name.NameTypeID
LEFT JOIN @tbName b ON b.NameID=Name.NameID
WHERE a.NameTypeID IS NOT NULL OR b.NameID IS NOT NULL