MYSQL通过两个引用表

时间:2017-11-02 18:24:08

标签: mysql

我对这个问题失去了理智,研究没有帮助。这是一种反模式和硬性因为这个,还是仅仅是我......

这里的IDEA是我们使用的每种材料都有一个表,我们有多个存储架,其中存储了该材料。每个库存位置都有单独的数量和最小限制。

我想要实现的是一个输出它的SQL查询:

+-------+------+---------------------+----------+-----------------+
| 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 |
+-------+------+---------------------+----------+-----------------+

这就是我现在所处的地方......

+-------+------+---------------------+----------+-----------------+
| 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 |
+-------+------+---------------------+----------+-----------------+

以下是表格:

材料

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

StockLocation

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

广告

+----+------------------+-------------+---------------------+----------+
| 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 |
+----+------------------+-------------+-----------------+

这是失败背后的怪物,

 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

1 个答案:

答案 0 :(得分:0)

事实证明,就像在那些产生NULL值的字段上添加MAX()一样简单。

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;