Bloggrammer
Bloggrammer

Reputation: 1111

Concatenate two fields from different tables in SQLite

How can I concatenate two fields from two different tables in SQLite?

Say I have:

Id  Name                
----------  ------
1           John         
2           Doe

Id    Name                
----------  ------
1           SchoolA
2           SchoolB

Expected Output

Id   Name                
----------  ------
1           John-SchoolA         
2           Doe-SchoolB

NB: Both tables have the same Ids.

Updated:

Table 1

 Id        Name                
----     -------
 1         John         
 2         Doe

Table 2

Table1_id   Name                
---------   ------
1           SchoolA
2           SchoolB

NB: Both tables have the same Ids (in terms of value).

Upvotes: 2

Views: 539

Answers (1)

forpas
forpas

Reputation: 164099

You must join the tables on their ids and concatenate the names of the matching rows:

SELECT t1.Id, 
       t1.Name || '-' || t2.Name AS Name
FROM table1 t1 INNER JOIN table2 t2
ON t2.Id = t1.Id

See the demo.

Upvotes: 1

Related Questions