xCloudx8 xCloudx8 - 1 year ago 94
SQL Question

Grab from table values

This is my table dataset:

col1 col2 col3 col4
2 7 5 3
11 18 31 7

Those are id's and need to use them as id to grab the name at them associated.
if i do:

select table1.name from table1,table2 where table2.id = 1

Gives me the name associated to id 1.
I want to automatically grab the names based on the id's in my table in this form.

name1 name2 name3 name4
name_id2 name_id7 name_id5 name_id3

There is a way to obtain this?



col2 col3 col4
2 3 4
2 5 6


nome id
all 1
dis1 2
dis3 3
dis4 4
dis5 5
dis6 6


col2 col3 col4
dis2 dis3 dis4
dis2 dis5 dis6

Answer Source

Try this code:

SELECT T2_1.nome as col2, T2_2.nome as col3, T2_3.nome as col4
FROM Table1
LEFT JOIN Table2 as T2_1 on T2_1.ID=Table1.col2
LEFT JOIN Table2 as T2_2 on T2_2.ID=Table1.col3
LEFT JOIN Table2 as T2_3 on T2_3.ID=Table1.col4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download