Not working
SELECT a.name, atn.name
FROM t1 a
JOIN t2 ap ON a.id = ap.area_id
JOIN t3 atn ON atn.id = ap.parent_id
I have a table t1 with area names and their type (like pin, ward and simple area name) and table t2 with their mapping and table t3 with type name and their id's.
I want a result with three columns (area name, pin, ward) i.e the result should be which area comes under pin and ward.
t1
:
--------------------------
| id | area name | type |
---------------------------
| 1 | a | 5 |
| 2 | b | 8 |
| 3 | x | 7 |
| 4 | z | 8 |
| 5 | pq | 8 |
---------------------------
t2
:
------------------------------
| id | area_id | parent_id |
------------------------------
| 1 | 2 | 1 |
| 2 | 2 | 3 |
| 3 | 4 | 1 |
| 4 | 5 | 3 |
-----------------------------
t3
:
------------------
| id | name |
------------------
| 5 | pin |
| 7 | ward |
| 8 | area |
------------------
Result:
--------------------------
| area | pin | ward |
--------------------------
| b | a | x |
| z | a | |
| pq | | x |
--------------------------
Anybody knows how to get this, please help me. I don't know how to get that value. I tried but couldn't find anything.