How would I convert the following to use INNER JOIN rather than nested SELECT?
SELECT
[Name].[NameV开发者_运维技巧alueID],
[Name].[NameTypeID],
[Name].[NameID],
[Name].[Value]
FROM [Name]
WHERE Name.NameTypeID IN ( SELECT NameTypeID FROM @tbNameType )
OR Name.NameID IN ( SELECT NameID FROM @tbName)
This one's tricky because it's an "OR" condition rather than an "AND". But I think this would do it:
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
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
精彩评论