sql-server – SQL Server – 包含多个字段的IN子句

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

 

posted on   癫狂编程  阅读(820)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2021-01-10 TPaintBox的前世今生
2021-01-10 如何从Delphi7使用VFrames(TVideoImage)从网络摄像头获取快照(How to get a snapshot from a webcam with Delphi7 using VFrames(TVideoImage))
2021-01-10 解析Delphi中的LoadLibrary,GetProcAddress,FreeLibrary
2021-01-10 确定要包含的Delphi运行时程序包(Determining Delphi Runtime Packages to Include)
2021-01-10 在Internet传播声音(二)(3)
2021-01-10 Delphi 内存分配 StrAlloc New(转)
2021-01-10 delphi 7 升级 到 xe2 CHAR类型变化

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示