SQL中的IN运算符用于连接的数值。

huangapple go评论62阅读模式
英文:

SQL IN Operator for Concatenated values

问题

Sure, here is the translated code snippet for your request:

我有几个代码,我需要检查这些代码是否在`News_Code``News_Digit`部分中。我的代码如下:

'LBX 345','NXC 670'

我尝试过的方法:

SELECT * 
FROM tblNews 
WHERE News_Code IN ('LBX 345', 'NXC 670') // 但问题在于我们不仅需要考虑News_Code,还需要考虑News_Digit

Please note that the translated code is provided as requested. If you have any more specific questions or need further assistance, feel free to ask.

英文:
ID News_Code News_Digits
1 GBX 400
2 NXC 670
3 LBO 880

I have several codes, what I need to check that code are in News_Code & news_Digit section. My code is like this:

'LBX 345','NXC 670'

What I have tried:

SELECT * 
FROM tblNews 
WHERE News_Code IN ('LBX 345', 'NXC 670')` // But here is the problem is not only the news_code we need to consider the News_Digit as well.

答案1

得分: 2

你可以连接这两列然后进行比较:

SELECT * 
FROM tblNews 
WHERE News_Code + ' ' + News_Digits IN ('LBX 345', 'NXC 670')

如果 News_Digits 不是字符串数据类型,你可以通过转换来改变它:

SELECT * 
FROM tblNews 
WHERE News_Code + ' ' + CAST(News_Digits AS VARCHAR(3)) IN ('LBX 345', 'NXC 670')
英文:

You can concatenate the two columns and then compare:

SELECT * 
FROM tblNews 
WHERE News_Code + ' ' + News_Digits IN ('LBX 345', 'NXC 670')

If News_Digits is not a string data type, you can change that by casting it:

SELECT * 
FROM tblNews 
WHERE News_Code + ' ' + CAST(News_Digits AS VARCHAR(3)) IN ('LBX 345', 'NXC 670')

答案2

得分: 1

你可以加入一个VALUES虚拟表(或使用一个真实的表变量、表值参数或临时表)

SELECT n.*
FROM tblNews n
JOIN (VALUES
  ('LBX', 345),
  ('NXC', 670)
) v(News_Code, News_Digits)
  ON v.News_Code = n.News_Code
 AND v.News_Digits = n.News_Digits;
英文:

You can join a VALUES virtual table (or use a real table variable, Table Valued Parameter or temp table)

SELECT n.*
FROM tblNews n
JOIN (VALUES
  ('LBX', 345),
  ('NXC', 670)
) v(News_Code, News_Digits)
  ON v.News_Code = n.News_Code
 AND v.News_Digits = n.News_Digits;

huangapple
  • 本文由 发表于 2023年5月30日 02:24:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定