I have a list table.
locations_list
id | location | table
----------------------
1 | Location 1 | location_1
2 | Location 2 | location_2
3 | Location 3 | location_3
location_1
delivered_time | item_name
-----------------------------------------------
2017-06-08 16:37:57 | Bag 1
2017-06-08 19:37:57 | Bag 2
location_2
delivered_time | item_name
-----------------------------------------------
2017-06-08 15:37:57 | Bag 3
2017-06-08 20:37:57 | Bag 4
output expected
id | location | table | delivered_time | item_name
------------------------------------------------------------------
1 | Location 1 | location_1 | 2017-06-08 16:37:57 | Bag 2
2 | Location 2 | location_2 | 2017-06-08 20:37:57 | Bag 4
3 | Location 3 | location_3 | |
The join table names are in first table. I don't have option to put all delivery info into one table and do a where
clause.
I tried UNION
. But I don't know the table names. It is available in location table only.
How can I solve this?
dbfiddle link
Best Answer
Using the following test data, you may find the procedure underneath useful. It will need some adjustments. However, it may give you a starting point (MySQL 5.7).
Test data:
Table for storing the procedure's output:
Draft version of the procedure:
Output (just select from the output_ table):
This may look a bit different from what you'd expect. However, in your dbfiddle, you had (in the loc_list): location_table_name 'silo_1' and 'silo_2', whereas the timestamps and bags were INSERTed into the tables called 'location_1' and 'location_2', as @RDFozz has pointed out.