从 table 个值中获取
Grab from table values
这是我的 table 数据集:
col1 col2 col3 col4
2 7 5 3
11 18 31 7
这些是 id,需要将它们用作 id 以获取与它们关联的名称。
如果我这样做:
select table1.name from table1,table2 where table2.id = 1
给我与 id 1 关联的名称。
我想以这种形式根据我 table 中的 id 自动获取名称。
name1 name2 name3 name4
name_id2 name_id7 name_id5 name_id3
有办法获得吗?
----------如所问示例
表 1:
col2 col3 col4
2 3 4
2 5 6
表 2:
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
怎么样?
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
试试这个代码:
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
这是我的 table 数据集:
col1 col2 col3 col4
2 7 5 3
11 18 31 7
这些是 id,需要将它们用作 id 以获取与它们关联的名称。 如果我这样做:
select table1.name from table1,table2 where table2.id = 1
给我与 id 1 关联的名称。 我想以这种形式根据我 table 中的 id 自动获取名称。
name1 name2 name3 name4
name_id2 name_id7 name_id5 name_id3
有办法获得吗?
----------如所问示例
表 1:
col2 col3 col4
2 3 4
2 5 6
表 2:
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
怎么样?
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
试试这个代码:
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