Search code examples
mysqlinner-join

How to concat values in different tables - mysql


I do not speak English, but I always try to learn. Sorry for bad interpretations.

I have a database with 2 tables. This tables are interconnected. See example:

table1

uniqueid,name,type,accountcode
9999999,test,incoming,1

table2

id,name,foo,bar
1, mobile call,foo,bar

In table one, the value accountcode is the same in table two id field. I would like execute a select and show this

table1

uniqueid,name,type,accountcode
9999999,test,incoming,**mobile call**

I'm try INNER JOIN, but the results did not go as expected.

Thanks!!


Solution

    • Join between the two tables using Inner Join with their appropriate relationships.
    • In case of multi-table queries, it is a good practice to use Aliasing, for code disambiguation and readability.

    Try the following query:

    SELECT 
      t1.uniqueid, 
      t1.name, 
      t1.type, 
      t2.name AS accountcode 
    FROM 
      Table1 AS t1 
    JOIN Table2 AS t2 ON t2.id = t1.accountcode