SQL Query在Inner JOIN期间连接两列

时间:2021-03-15 07:57:14

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.

2 个解决方案

#1


14  

Try this:

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

#2


1  

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.

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

* Similar Question

*类似的问题

#1


14  

Try this:

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

#2


1  

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.

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

* Similar Question

*类似的问题