user2429282
user2429282

Reputation: 13

How can I select from different tables in one MySQL query?

table1
id firstname
-------------
1 Elon
2 Steve


table2
id profession
-------------
1 Entrepreneur
2 Engineer


table3
firstname profession
-------------
1 2
2 1

Need result:

firstname profession
-------------
Elon Engineer
Steve Entrepreneur

How can I select from different tables in one MySQL query? How can I select from different tables in one MySQL query?

Upvotes: 0

Views: 77

Answers (2)

Aleksandar Vasić
Aleksandar Vasić

Reputation: 593

You can do:

SELECT t1.firstname, t2.profession
   FROM table1 as t1
   LEFT JOIN table3 as t3 ON t1.id = t3.firstname
   LEFT JOIN table2 as t2 on t2.id = t3.profession

this one works, tested

Upvotes: 0

Jens
Jens

Reputation: 69440

This statement should give you the result you needed.

select t1.firstname, t2.profession from table1 t1 join table3 t3 on t1.id=t3.firstname join table2 t2 on t3.profession = t2.profession

Upvotes: 1

Related Questions