SQL Server - IN clause with multiple fields

后端 未结 8 699
野性不改
野性不改 2020-12-20 11:42

Is it possible to include in a IN clause multiple fields? Something like the following:

select * from user
where code, userType in ( select code         


        
相关标签:
8条回答
  • 2020-12-20 12:14

    Not the way you have posted. You can only return a single field or type for IN to work.

    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.
    

    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
    
    0 讨论(0)
  • 2020-12-20 12:16

    You can either use joins

    SELECT * FROM user U 
    INNER JOIN userType UT on U.code = UT.code 
    AND U.userType = UT.userType
    
    0 讨论(0)
提交回复
热议问题