xCloudx8
xCloudx8

Reputation: 721

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?

---------AS ASKED EXAMPLE

Table1:

col2 col3 col4
 2     3    4  
 2     5    6

Table2:

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

My_result:

col2 col3 col4
 dis2 dis3 dis4
 dis2 dis5 dis6

Upvotes: 0

Views: 126

Answers (2)

Marcin C.
Marcin C.

Reputation: 169

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

Upvotes: 1

Boris Schegolev
Boris Schegolev

Reputation: 3701

How about that?

SELECT t_name1.nome, t_name2.nome, t_name3.nome
FROM table1
LEFT JOIN table2 t_name1 ON table1.col2 = t_name1.id
LEFT JOIN table2 t_name2 ON table1.col3 = t_name2.id
LEFT JOIN table2 t_name3 ON table1.col4 = t_name3.id

Upvotes: 1

Related Questions