gpt4 book ai didi

MYSQL 内部连接 ​​2 个表和 sum()

转载 作者:行者123 更新时间:2023-11-29 01:03:43 26 4
gpt4 key购买 nike

加入 2 个表后我无法获得 sum(),一个是 HEADER AND DETAIL。对于下面的 sql 语句,它只给出 0 个结果。需要一些帮助:

我的表:

INVHDR:

    Invno,  Invdate,   Ac_code
100 2013-04-01 2
101 2013-04-30 2

INVDTLS:
Invno, Prod_desc, Amount
100 Argon 155
100 Argon 250
101 Oxygen 322
101 Oxygen 065

期望的结果: 总金额:405

MYSQL 语句内连接和 sum()

SELECT
a.Invno, a.Ac_code, a.Invdate, b.*
FROM INVHDR a
INNER JOIN (
SELECT
Invno, Prod_desc, SUM( Amount ) AS amts
FROM INVDTLS
WHERE Prod_desc='Argon'
) AS b ON a.Invno = b.Invno
WHERE
a.Ac_code='2'
AND a.Invdate BETWEEN '2013-04-01'
AND '2013-04-30'
GROUP BY a.Ac_code

最佳答案

你为什么要写这样一个复杂 查询,试试这个:

SELECT sum(b.Amount)
FROM INVHDR a
INNER JOIN INVDTLS b
ON a.Invno = b.Invno
WHERE a.Ac_code='2'
AND a.Invdate BETWEEN '2013-04-01' AND '2013-04-30'
AND b.Prod_desc='Argon'
--Group by b.Prod_desc,a.Invno, a.Ac_code

这是 SQL Fiddle

关于MYSQL 内部连接 ​​2 个表和 sum(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com