Query the same search table with multiple columns

I am a little confused by this. I have a data table structured as follows:

Table: Data

DataID    Val
1         Value 1
2         Value 2
3         Value 3
4         Value 4

Then I have another table, structured as follows:

Table: Table 1

Col1    Col2
1       2
3       4
4       3
2       1

Both columns from table 1 indicate the data in the data table. How can I get this data in a request? For example, a request to return:

Query: Query1

Column1    Column2
Value 1    Value 2
Value 3    Value 4
Value 4    Value 3
Value 2    Value 1

I am familiar enough with SQL to make a single column join, but lost it. Any help is appreciated. An sql example or a link to something to read. Thank!

PS: This is in sqlite

+3
source share
1 answer

You can join the same table twice:

Select
  d1.val As column1,
  d2.val As column2
From table1 t
Join data d1 On ( d1.dataId = t.col1 )
Join data d2 On ( d2.dataId = t.col2 )
+11

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


All Articles