HellOfACode
HellOfACode

Reputation: 1743

MySQL combining 3 tables in query

I have three tables.

StockinId - all shelfs in location
+---------+----------+-------+
| country | location | bin   |
+---------+----------+-------+
| LV      | AL       | A-1-1 |
| LV      | AL       | A-1-2 |
| LV      | AL       | A-1-3 |
| LV      | AL       | A-1-4 |
| LV      | AL       | A-1-5 |
| LV      | AL       | A-1-6 |
| LV      | AL       | A-1-7 |
| LV      | AL       | A-1-8 |
| LV      | AL       | A-1-9 |
+---------+----------+-------+


Calculated - calculated amount which should be on the shelf 
+-------------------+----------+-------+
| calculated_needed | location | bin   |
+-------------------+----------+-------+
| 2                 | AL       | A-1-1 |
| 4                 | AL       | A-1-2 |
| 15                | AL       | A-1-3 |
| 5                 | AL       | A-1-4 |
| 22                | AL       | A-1-5 |
+-------------------+----------+-------+

Inventory - actual amount counted physically recounting
+--------+----------+-------+---------------+
| amount | location | bin   | item          |
+--------+----------+-------+---------------+
| 2      | AL       | A-1-1 | 2600000741897 |
| 4      | AL       | A-1-2 | 2600000741897 |
| 5      | AL       | A-1-4 | 2600000999045 |
| 22     | AL       | A-1-5 | 2600000998949 |
| 6      | AL       | A-1-6 | 2600000998925 |
| 99     | AL       | A-1-7 | 2600000998871 |
+--------+----------+-------+---------------+

I want to create a query where the output would be containing all bins from the calculated table and the rows of inventory table that have products counted on them even though the bins should be empty aka items have been displaced in real life. The example is visible below. I have tried but do not have the knowledge to do this. I can do a query where i see all the calculated bins with physical and calculated amounts accordingly but i cant get to output the misplaced but counted items on bins that are not in Calculated table.

+-------+----------------+-------------------+
| bin   | physcal_amount | calculated_amount |
+-------+----------------+-------------------+
| A-1-1 | 2              | 2                 |
| A-1-2 | 4              | 4                 |
| A-1-3 | -              | 15                |
| A-1-4 | 5              | 5                 |
| A-1-5 | 22             | 22                |
| A-1-6 | 6              | -                 |
| A-1-7 | 99             | -                 |
+-------+----------------+-------------------+

I sincerely hope i have made the issue clear enough to be understandable.

Upvotes: 0

Views: 31

Answers (1)

Romain B.
Romain B.

Reputation: 654

If i got your question right :

SELECT StockinId.bin, COALESCE(Inventory.amount,'-') as physcal_amount, COALESCE(Calculated.calculated_needed,'-') as calculated_amount FROM StockinId LEFT JOIN Calculated ON StockinId.bin = Calculated.bin LEFT JOIN Inventory ON StockinId.bin = Inventory.bin WHERE Calculated.calculated_needed IS NOT NULL OR Inventory.amount IS NOT NULL

Upvotes: 1

Related Questions