简体   繁体   English

SQL Query在Inner JOIN期间连接两列

[英]SQL Query Concatenate two columns during Inner JOIN

I have table A and table B with Table A having several columns including A1 and A2. 我有表A和表B,表A有几列,包括A1和A2。 Table B too has several columns. 表B也有几列。 My query requires me to concatenate the values in A1 and A2 and then do an inner join on B1. 我的查询要求我连接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. 谢谢。

Try this: 试试这个:

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

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类似的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM