简体   繁体   中英

MYSQL combine and select rows by two reference tables

I am losing my mind over this problem, research has not helped. Is this an anti-pattern and hard because of that, or is it just me...

The IDEA here is that we have a single table for every material we use, we have multiple storage shelf's in which that material is stored on. Every stocklocation has individual quantities and minimum limits.

What I want to achieve is a single SQL-query that outputs this:

+-------+------+---------------------+----------+-----------------+
| SL_Id | M_Id | TimeCreated         | Quantity | MinimumQuantity |
+-------+------+---------------------+----------+-----------------+
|     1 |    2 | 2017-11-02 13:04:18 |       10 |               5 |
|     1 |    3 | NULL                |     NULL |              15 |
|     1 |    4 | 2017-11-02 15:56:56 |        7 |            NULL |
+-------+------+---------------------+----------+-----------------+

This is where I am at the moment....

+-------+------+---------------------+----------+-----------------+
| SL_Id | M_Id | TimeCreated         | Quantity | MinimumQuantity |
+-------+------+---------------------+----------+-----------------+
|     1 |    2 | 2017-11-02 13:04:18 |       10 |            NULL |
|     1 |    3 | NULL                |     NULL |              15 |
|     1 |    4 | 2017-11-02 15:56:56 |        7 |            NULL |
+-------+------+---------------------+----------+-----------------+

Here are the tables:

Material

+----+-----------+
| Id | OrderCode |
+----+-----------+
|  2 | asdf      |
|  3 | 75424     |
|  4 | 45567     |
+----+-----------+

StockLocation

+----+-------+
| Id | Label |
+----+-------+
|  1 | asdf  |
+----+-------+

Inventory

+----+------------------+-------------+---------------------+----------+
| Id | StockLocation_Id | Material_Id | TimeCreated         | Quantity |
+----+------------------+-------------+---------------------+----------+
|  1 |                1 |           2 | 2017-11-02 13:04:18 |       10 |
|  2 |                1 |           4 | 2017-11-02 15:23:26 |        9 |
|  3 |                1 |           4 | 2017-11-02 15:56:56 |        7 |
+----+------------------+-------------+---------------------+----------+

StockLocationMaterialLimit

+----+------------------+-------------+-----------------+
| Id | StockLocation_Id | Material_Id | MinimumQuantity |
+----+------------------+-------------+-----------------+
|  1 |                1 |           2 |               5 |
|  2 |                1 |           3 |              15 |
+----+------------------+-------------+-----------------+

This is the monster behind failure,

 SELECT 
    SL_Id,
    M_Id,
    TimeCreated,
    Quantity,
    MinimumQuantity
    FROM (
        SELECT
        SL.Id AS SL_Id,
        M.Id AS M_Id,
        I.TimeCreated AS TimeCreated,
        I.Quantity AS Quantity,
        NULL AS MinimumQuantity
        FROM StockLocation SL, Material M
        JOIN Inventory I on I.Id = (
            SELECT Id FROM Inventory I1 WHERE I1.StockLocation_Id=SL.Id AND I1.Material_Id=M.Id ORDER BY I1.TimeCreated DESC LIMIT 1
        )
    UNION
    SELECT
        SL.Id AS SL_Id,
        M.Id AS M_Id,
        NULL AS TimeCreated,
        NULL AS Quantity,
        SLML.MinimumQuantity AS MinimumQuantity
        FROM StockLocation SL, Material M
        JOIN StockLocationMaterialLimit SLML on SLML.Id = (
            SELECT Id FROM StockLocationMaterialLimit SLML1 WHERE SLML1.StockLocation_Id=SL.Id AND SLML1.Material_Id=M.Id LIMIT 1
        )
) tst GROUP BY SL_Id,M_Id

It turned out to be as easy as adding MAX() on those fields which produced NULL values.

SELECT 
    M_Id,
    SL_Id,
    TimeCreated,
    MAX(Quantity) AS Quantity,
    MAX(MinimumQuantity) AS MinimumQuantity
    FROM (
        SELECT
        M.Id AS M_Id,
        SL.Id AS SL_Id,
        I.TimeCreated AS TimeCreated,
        I.Quantity AS Quantity,
        NULL AS MinimumQuantity
        FROM StockLocation SL, Material M
        INNER JOIN Inventory I on I.Id = (
            SELECT Id FROM Inventory I1 WHERE I1.StockLocation_Id=SL.Id AND I1.Material_Id=M.Id ORDER BY I1.TimeCreated DESC LIMIT 1
        )
    UNION
    SELECT
        M.Id AS M_Id,
        SL.Id AS SL_Id,
        NULL AS TimeCreated,
        NULL AS Quantity,
        SLML.MinimumQuantity AS MinimumQuantity
        FROM StockLocation SL, Material M
        INNER JOIN StockLocationMaterialLimit SLML on SLML.Id = (
            SELECT Id FROM StockLocationMaterialLimit SLML1 WHERE SLML1.StockLocation_Id=SL.Id AND SLML1.Material_Id=M.Id LIMIT 1
        )
) tst GROUP BY SL_Id, M_Id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM