olszamix
olszamix

Reputation: 63

SQL get name on the id from other table

I have 2 tables:

1.Table1

---------------------------------
| id_table1 | id_test1| id_test2|
---------------------------------
| 1         | 10      | 1       |
---------------------------------
| 2         | 20      | 3       |
---------------------------------

2.Table2

-------------------
| id_table2 | name|
-------------------
| 1         |name1| 
-------------------
| 3         |name3|  
-------------------
| 10        |name10| 
-------------------
| 20        |name20| 
-------------------

id_test1 and id_test2 have relation with table2 and I want to query only name, example

-------------------
| NameA     |NameB|
-------------------
| name10    |name1| 
-------------------
| name20    |name3| 
-------------------

in PHP I can solve loops but I'm interested to solve this only in SQL.

Upvotes: 1

Views: 2470

Answers (2)

Nadeem Abbas
Nadeem Abbas

Reputation: 15

SELECT a.name b.name FROM " + table2 +" a JOIN "+ table1 + " z ON a.id_table2 = z.id_test1 WHERE a.name ="+ "'"+given_ID(id_test1)+"'" and b.name ="+ "'"+given_ID(id_test2)+"'";

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

Should be this query

select a.name, b.name 
from table2 as a, 
inner join table2 as b
inner join table1 as c
where c.id_test1 = a.id_table2
and c.id_test2 = b.id_table2;

Upvotes: 1

Related Questions