I am having a problem with MySQL joins.
Table_A
:
A_id Cost1 A1_id Cost2
1 500 0 200
1 100 1 100
1 50 2 60
1 10 3 50
2 5 0 10
Table_B
(Refers B_id
: from Table_A
A1_id
):
B_id FName LName
1 X A
2 Y B
3 Z C
Table_C
(Refers C_id
: from Table_A
A_id
):
C_id Towns
1 Atlanta
2 NewYork
I need to combine all three tables like the following output:
- I extract the
Towns
that match(Table_A.A_id=Table_C.C_id)
. - I extract the
Fname
,Lname
that match(table_A.A1_id=Table_b.b_id)
. - I need to skip the
Towns
ifA1_id !=0
. - I need to skip the
Fname
,Lname
ifA1_id ==0
.
Remaining data may either be a value or null, which I specify as '#'. What would be an efficient MySQL query for the given scenario?
Output:
A_id Cost1 A1_id cost2 Fname Lname Towns
1 500 0 200 # # Atlanta
1 100 1 100 X A #
1 50 2 60 Y B #
1 10 3 50 Z C #