本文介绍了SQL Server-具有多个字段的IN子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在IN
子句中包含多个字段?类似于以下内容:
Is it possible to include in a IN
clause multiple fields? Something like the following:
select * from user
where code, userType in ( select code, userType from userType )
我正在使用ms sql server 2008
I'm using ms sql server 2008
我知道这可以通过联接来实现并且存在,我只是想知道是否可以使用IN
子句来完成.
I know this can be achieved with joins and exists, I just wanted to know if it could just be done with the IN
clause.
推荐答案
不是您发布的方式.您只能返回单个字段或键入IN
才能正常工作.
Not the way you have posted. You can only return a single field or type for IN
to work.
从MSDN( IN
):
From 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
:
Instead of IN
, you could use a JOIN
using the two fields:
SELECT U.*
FROM user U
INNER JOIN userType UT
ON U.code = UT.code
AND U.userType = UT.userType
这篇关于SQL Server-具有多个字段的IN子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!