我正在尝试alldocs中的所有文件(其中siteid = something和checkout不为null)。 Alldocs有一个调用checkoutuserid的字段,我需要从userinfo表中获取名称。 UserInfo可能重复相同的ID,这就是我添加site-id的原因。我在以下查询中收到错误。 "子查询返回的值超过1)
SELECT AllDocs.LeafName
, AllDocs.CheckoutDate
, UserInfo.tp_Title AS [User Name]
, UserInfo.tp_Login AS [User ID]
, UserInfo.tp_Email
FROM AllDocs
INNER JOIN UserInfo ON AllDocs.CheckoutUserId = (
SELECT UserInfo.tp_ID
FROM UserInfo
WHERE UserInfo.tp_SiteID = 'E2FF98A7-B719-428D-8C30-856F08989691'
)
WHERE (AllDocs.SiteId = 'E2FF98A7-B719-428D-8C30-856F08989691')
AND (AllDocs.CheckoutUserId IS NOT NULL)
ORDER BY AllDocs.LeafName
我正在添加以下工作代码,因为我已在查询中添加了第3个表。以下是完美的工作。
use WSS_Content_MMRHome1
SELECT distinct AllDocs.LeafName AS [File Name], AllDocs.dirName AS [Path], AllDocs.CheckoutDate,
UserInfo.tp_Title AS [User Name], UserInfo.tp_Login AS [User ID],
UserInfo.tp_Email AS [User Email], AllLists.tp_Title AS [List Name], ('http://inside.nv.com/'+AllDocs.DirName+'/'+AllDocs.LeafName) AS URL
FROM AllDocs
INNER JOIN UserInfo ON AllDocs.CheckoutUserId = UserInfo.tp_ID AND UserInfo.tp_SiteID = AllDocs.SiteId
INNER JOIN AllLists ON AllDocs.ListId = AllLists.tp_ID
WHERE AllDocs.SiteId = 'D36CCEA0-5351-4AEC-8B83-ACA2439CF38B'
AND AllDocs.CheckoutUserId IS NOT NULL
ORDER BY AllDocs.LeafName
答案 0 :(得分:3)
不需要子查询,只需将其他条件移至join
(或包含在where
子句中):
SELECT distinct AllDocs.LeafName, AllDocs.CheckoutDate,
UserInfo.tp_Title AS [User Name], UserInfo.tp_Login AS [User ID],
UserInfo.tp_Email
FROM AllDocs INNER JOIN
UserInfo ON AllDocs.CheckoutUserId = UserInfo.tp_ID
AND UserInfo.tp_SiteID = 'E2FF98A7-B719-428D-8C30-856F08989691'
WHERE AllDocs.SiteId = 'E2FF98A7-B719-428D-8C30-856F08989691'
AND AllDocs.CheckoutUserId IS NOT NULL
ORDER BY AllDocs.LeafName
实际上看起来像是同一个网站ID?如果是这样,也可以加入该领域:
...
FROM AllDocs INNER JOIN
UserInfo ON AllDocs.CheckoutUserId = UserInfo.tp_ID
AND UserInfo.tp_SiteID = AllDocs.SiteId
WHERE AllDocs.SiteId = 'E2FF98A7-B719-428D-8C30-856F08989691'
AND AllDocs.CheckoutUserId IS NOT NULL
ORDER BY AllDocs.LeafName