sql-server – SQL Server – 包含多個欄位的IN子句
阿新 • • 發佈:2022-01-10
是否可以在IN子句中包含多個欄位?類似於以下內容:
select * from user where code,userType in ( select code,userType from userType )
我正在使用mssqlserver 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好的程式碼像粥一樣,都是用時間熬出來的