SQL SQL查询在内部联接期间连接两列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9828046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:57:41  来源:igfitidea点击:

SQL Query Concatenate two columns during Inner JOIN

sqlsql-servertsqljoinconcatenation

提问by Niras

I have table A and table B with Table A having several columns including A1 and A2. Table B too has several columns. My query requires me to concatenate the values in A1 and A2 and then do an inner join on B1.

我有表 A 和表 B,表 A 有几列,包括 A1 和 A2。表 B 也有几列。我的查询要求我连接 A1 和 A2 中的值,然后在 B1 上进行内部连接。

Example:

例子:

Select * 
From A
INNER JOIN B
ON CONCAT(A1,A2) = B1.

Apparently this is not how it should work. Can someone please give me a hand in this query?

显然这不是它应该如何工作。有人可以帮我解决这个问题吗?

Thanks.

谢谢。

回答by Andrey Gurinov

Try this:

尝试这个:

Select *  
From A 
INNER JOIN B 
ON A1 + A2 = B1

回答by Pedro Ferreira

Sample taken from

样本取自

Table Geography

表地理

region_name store_name
East    Boston
East    New York
West    Los Angeles
West    San Diego

Example 1: For MySQL/Oracle:

示例 1:对于 MySQL/Oracle:

    SELECT CONCAT(region_name,store_name) FROM Geography 
    WHERE store_name = 'Boston';
Result: 'EastBoston'

Example 2: For Oracle:

示例 2:对于 Oracle:

    SELECT region_name || ' ' || store_name FROM Geography 
    WHERE store_name = 'Boston';
Result: 'East Boston'

Example 3: For SQL Server:

示例 3:对于 SQL Server:

    SELECT region_name + ' ' + store_name FROM Geography 
    WHERE store_name = 'Boston';
Result: 'East Boston'

Starting from this, you can adapt to two tables without much issue. In doubt, use a virtual Table to make things more readable.

从这里开始,您可以适应两个表而没有太大问题。有疑问,使用虚拟表使事情更具可读性。

If in doubt check this other question which has been answered for more details.

如果有疑问,请查看已回答的其他问题以获取更多详细信息。

StackOverFlow Similar Question

StackOverFlow 类似问题