SQL Query join two columns during Inner JOIN

I have table A and table B with table A having multiple columns including A1 and A2. Table B also has several columns. My query requires me to concatenate the values ​​in A1 and A2, and then do the inner join on 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 request?

Thanks.

+4
source share
2 answers

Try the following:

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

Example from

Geography tables

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

Example 1: For MySQL / Oracle:

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

Example 2: For Oracle:

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

Example 3: For 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 any problems. In doubt, use a virtual table to make things more readable.

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

fooobar.com/questions/1402951 / ...

+1
source

Source: https://habr.com/ru/post/1402950/


All Articles