我正在使用SQL Server 2005。
查询看起来像这样
Select col1, col2, col3 from <subquery> where (col1,col2) in <subquery>
SQL Server似乎不喜欢这样。任何实现任何人都知道的方法都不涉及转换为varchars或其他任何混乱?
这是实际查询。
SELECT *
FROM
(
SELECT NEWID() AS guid, UserID, RoleId, ClubId, 0 AS GroupRole
FROM dbo.Portal_UserRoles
UNION
SELECT NEWID() AS guid, dbo.Portal_UserGroups.UserId,
dbo.Portal_GroupRoles.RoleId, dbo.Portal_UserGroups.ClubId,
dbo.Portal_GroupRoles.GroupId AS GroupRole
FROM dbo.Portal_GroupRoles
INNER JOIN dbo.Portal_UserGroups
ON dbo.Portal_GroupRoles.GroupId = dbo.Portal_UserGroups.GroupId
) AS derivedtbl_1
WHERE (derivedtbl_1.RoleId,derivedtbl_1.ClubId) IN
(
SELECT Portal_GroupRoles.RoleId, Portal_ClubGroups.ClubId
FROM Portal_GroupRoles
INNER JOIN Portal_ClubGroups
ON Portal_GroupRoles.GroupId = Portal_ClubGroups.GroupId
)
答案 0 :(得分:13)
您所寻求的标准,经典方式是EXISTS条款:
SELECT *
FROM
(
SELECT NEWID() AS guid, UserID, RoleId, ClubId, 0 AS GroupRole
FROM dbo.Portal_UserRoles
UNION
SELECT NEWID() AS guid, dbo.Portal_UserGroups.UserId,
dbo.Portal_GroupRoles.RoleId, dbo.Portal_UserGroups.ClubId,
dbo.Portal_GroupRoles.GroupId AS GroupRole
FROM dbo.Portal_GroupRoles
INNER JOIN dbo.Portal_UserGroups
ON dbo.Portal_GroupRoles.GroupId = dbo.Portal_UserGroups.GroupId
) AS derivedtbl_1
WHERE EXISTS
(
SELECT Portal_GroupRoles.RoleId, Portal_ClubGroups.ClubId
FROM (Portal_GroupRoles
INNER JOIN Portal_ClubGroups
ON Portal_GroupRoles.GroupId = Portal_ClubGroups.GroupId) AS cgr
WHERE derivedtbl_1.RoleID = cgr.RoleId
AND derivedtbl_1.ClubId = cgr.ClubId
)
要小心将两列条件分成两个独立的IN子句;在一个EXISTS子句中应用双列条件时,它没有给出相同的答案(一般)。
答案 1 :(得分:5)
在派生表上进行连接,而不是使用in
SELECT *
FROM
(
SELECT NEWID() AS guid, UserID, RoleId, ClubId, 0 AS GroupRole
FROM dbo.Portal_UserRoles
UNION
SELECT NEWID() AS guid, dbo.Portal_UserGroups.UserId,
dbo.Portal_GroupRoles.RoleId, dbo.Portal_UserGroups.ClubId,
dbo.Portal_GroupRoles.GroupId AS GroupRole
FROM dbo.Portal_GroupRoles
INNER JOIN dbo.Portal_UserGroups
ON dbo.Portal_GroupRoles.GroupId = dbo.Portal_UserGroups.GroupId
) AS derivedtbl_1
INNER JOIN
(
SELECT Portal_GroupRoles.RoleId, Portal_ClubGroups.ClubId
FROM Portal_GroupRoles
INNER JOIN Portal_ClubGroups
ON Portal_GroupRoles.GroupId = Portal_ClubGroups.GroupId
) derivedtbl_2
ON derivedtbl_1.RoleID = derivedtbl_2.RoleID
AND derivedtbl_1.ClubId = derivedtbl_2.ClubId
答案 2 :(得分:3)
SELECT
/*
your selected fields, joins here
*/
WHERE -- (derivedtbl_1.RoleId,derivedtbl_1.ClubId) IN
EXISTS
(
-- actually you can change these two fields to * (asterisk ) or 1, whatever, even your name, what matters only is the testing of existence(see below)
SELECT Portal_GroupRoles.RoleId, Portal_ClubGroups.ClubId
FROM Portal_GroupRoles
INNER JOIN Portal_ClubGroups
ON Portal_GroupRoles.GroupId = Portal_ClubGroups.GroupId
-- here is your IN (the testing of existence):
WHERE Portal_GroupRoles.RoleId = derivedtbl_1.RoleId AND
AND derivedtbl_1.ClubId = derivedtbl_1.ClubId
)
alternatively:
SELECT
/*
your selected fields, joins here
*/
JOIN
(
SELECT Portal_GroupRoles.RoleId, Portal_ClubGroups.ClubId
FROM Portal_GroupRoles
INNER JOIN Portal_ClubGroups
ON Portal_GroupRoles.GroupId = Portal_ClubGroups.GroupId
) X
-- here is your IN:
ON X.RoleId = derivedtbl_1.RoleId
AND X.ClubId = derivedtbl_1.ClubId
答案 3 :(得分:2)
您的语法很有用,标准SQL支持。它尚未在SQL Server中实现,但您可以投票选择其包含here。
答案 4 :(得分:-3)
你必须将两个条款分开
where col1 in (...) AND col2 in (...)
或者你可以稍微重构一下
select * FROM (
SELECT NEWID() AS guid, UserID, RoleId, ClubId, 0 AS GroupRole FROM dbo.Portal_UserRoles
UNION
SELECT NEWID() AS guid, dbo.Portal_UserGroups.UserId, dbo.Portal_GroupRoles.RoleId, dbo.Portal_UserGroups.ClubId, dbo.Portal_GroupRoles.GroupId AS GroupRoles FROM dbo.Portal_GroupRoles INNER JOIN dbo.Portal_UserGroups ON dbo.Portal_GroupRoles.GroupId = dbo.Portal_UserGroups.GroupId)
AS derivedtbl_1, Portal_GroupRoles, Portal_ClubGroup
where derivedtbl_1.RoleId = Portal_GroupRoles.RoleId
and derivedtbl_1.ClubId = Portal_ClubGroups.ClubId
and Portal_GroupRoles.GroupId = Portal_ClubGroups.GroupId