【发布时间】:2026-01-11 03:45:01
【问题描述】:
有没有办法通过连接 3 个表来查找 SQL Query 并在一段时间内执行一些操作? 如下查询:
https://i.stack.imgur.com/SpWkt.png
SELECT ST.code,ST.Plant,ST.STINT,E.code,E.Plant,SUM(E.Quantite) AS E_Qte,S.code,S.Plant,S.S_Qte
FROM `enter` E
INNER JOIN (SELECT code,Plant,SUM(Quantite) AS S_Qte FROM `sortie` group by code,Plant) S
INNER JOIN
(SELECT code,plant,sum(stockinitiale) AS STINT
FROM `stockinitiale` group by code,Plant) ST ON E.Code= S.Code=ST.Code AND E.Plant= S.Plant=ST.Plant
WHERE date BETWEEN '2019-05-17' AND '2019-05-28'
GROUP BY E.code,S.code,ST.code,E.Plant,S.Plant,ST.Plant
预期输出:
+------+-------+-------+------+-------+-------+------+-------+--------+
| code | Plant | STINT | code | Plant | E_Qte | code | Plant | S_Qte |
+------+-------+-------+------+-------+-------+------+-------+--------+
| 1001 | P1 | 122 | 1001 | P1 | 662 | 1001 | P1 | 19 |
| 1001 | P3 | 108 | 1001 | P3 | 10 | 1001 | P3 | 2 |
| 1002 | P1 | 0 | 1002 | P1 | 4 | 1002 | P1 | 15 |
| 1002 | P3 | 6 | 1002 | P3 | 7 | 1002 | P3 | 12 |
| 1008 | P1 | 12 | 1001 | P1 | 66 | 1001 | P1 | 17 |
+------+-------+-------+------+-------+-------+------+-------+--------+
【问题讨论】:
-
这是 E.Plant= S.Plant=ST.Plant 上的错字吗?并且您的第一次加入需要一个 on 子句
-
首先感谢您的评论。此查询有效,但是当我添加 E.code= S.code=ST.code 时,我得到了空显示!
标签: mysql sql sql-server phpmyadmin