英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论