Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL syntax multiple joins?

Tags:

sql

I have a two tables,Transaction and Master. The transaction table shows a from and to activity id. The master shows the activity id and a name. The from and to id use the same master table to associate the activity id and name. What I would like to do is take the distinct from and to values and display them with the associated name.

The original query was

select distinct a.from, a.to from Transaction a

What I need is something where a.from is followed by b.name and a.to is followed by b.name

I know that I have to have a join but I need the join to apply to each of the distinct a.values.

In concept I would like to do 2 joins with one on each of the a.values but I am not sure how to delineate the from and to values.

like image 898
Tim Vavra Avatar asked Aug 05 '13 19:08

Tim Vavra


1 Answers

select distinct a.from, f.name as FromName, a.to, t.name as ToName 
from Transaction a
join Master F on a.from = f.id
join Master T on a.to = f.id
like image 70
HLGEM Avatar answered Oct 24 '22 06:10

HLGEM