I have four tables - tblBase
, tblLookup
, tblData
and tblData2
tblBase
+---------+----------+-----------+------------+
| Base_ID | Base_Num | Base_Type | Base_Date |
+---------+----------+-----------+------------+
| 1 | 1234 | ABC | 01/05/2016 |
| 2 | 3456 | DEF | 02/05/2016 |
| 3 | 7890 | GHI | 03/05/2016 |
+---------+----------+-----------+------------+
tblLookup
+-----------+-------------+
| Lookup_ID | Lookup_Name |
+-----------+-------------+
| 1 | Apple |
| 2 | Orange |
| 3 | Banana |
+-----------+-------------+
tblData
+-----------+----------+------------+
| Data_Name | Data_Num | Data_Date |
+-----------+----------+------------+
| Apple | 1234 | 02/05/2016 |
| Orange | 3456 | 03/05/2016 |
| Guava | 5937 | 04/05/2016 |
+-----------+----------+------------+
tblData2
+------------+-----------+------------+
| Data2_Name | Data2_Num | Data2_Date |
+------------+-----------+------------+
| Grapes | 3953 | 02/05/2016 |
| Orange | 3456 | 03/05/2016 |
| Banana | 7890 | 04/05/2016 |
| Banana | 1473 | 07/05/2016 |
+------------+-----------+------------+
I am trying to get the Data_Date
from tblData
or tblData2
(where ever the data exists) join with tblBase
where Base_Num
matches . As the common columns exists in tblLookup
, I need to join all the four tables.
For example, Base_ID = 3
, Base_Num = 7890
, should pick up Data_Date
from tblData2
, as both Base_ID (Banana)
and Base_Num (7890)
matches.
I tried doing INNER JOIN
however it did not give the desired result.
I'm looking for a resulting table like this:
+---------+----------+-----------+------------+-------------------+
| Base_ID | Base_Num | Base_Type | Base_Date | Desired_Data_Date |
+---------+----------+-----------+------------+-------------------+
| 1 | 1234 | ABC | 01/05/2016 | 02/05/2016 |
| 2 | 3456 | DEF | 02/05/2016 | 03/05/2016 |
| 3 | 7890 | GHI | 03/05/2016 | 04/05/2016 |
+---------+----------+-----------+------------+-------------------+