是否可以在IN子句中包含多个字段?类似于以下内容:
select * from user where code,userType in ( select code,userType from userType )
我正在使用ms sql server 2008
我知道这可以通过连接和存在来实现,我只是想知道它是否可以用IN子句完成.
解决方法
不是你发布的方式.您只能为IN返回单个字段或类型才能工作.
来自MSDN(IN
):
test_expression [ NOT ] IN ( subquery | expression [,...n ] ) subquery - Is a subquery that has a result set of one column. This column must have the same data type as test_expression. expression[,... n ] - Is a list of expressions to test for a match. All expressions must be of the same type as test_expression.
您可以使用以下两个字段来使用JOIN而不是IN:
SELECT U.* FROM user U INNER JOIN userType UT ON U.code = UT.code AND U.userType = UT.userType