我正在使用SQL Server 2005.
查询看起来像这样
Select col1, col2, col3 fromwhere (col1,col2) in
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 )
Jonathan Lef.. 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子句中应用两列条件时,不会给出相同的答案(一般).
执行所需操作的标准,经典方法是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子句中应用两列条件时,不会给出相同的答案(一般).
在派生表上进行连接而不是使用 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