是否可以在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

 

相关文章:

  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2021-11-28
  • 2022-12-23
猜你喜欢
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2022-03-02
  • 2021-09-03
  • 2021-09-25
相关资源
相似解决方案