Sql 不同表格中的总数量

Sql 不同表格中的总数量,sql,Sql,捐赠=给予机构的东西。 选矿=机构给予有需要的人的福利。 我需要实现“表预期”。我尝试了sum。我不太懂SQL,如果有人能帮我,那就太好了。试着把两者的总和相加 ProdStock +---------+--------------+ | ID_Prod | Description | +---------+--------------+ | 1 | tshirt | | 2 | pants | | 3 | hat

捐赠=给予机构的东西。
选矿=机构给予有需要的人的福利。

我需要实现“表预期”。我尝试了
sum
。我不太懂SQL,如果有人能帮我,那就太好了。

试着把两者的总和相加

ProdStock
+---------+--------------+
| ID_Prod |  Description |
+---------+--------------+
|    1    |    tshirt    |
|    2    |    pants     |
|    3    |      hat     |
+---------+--------------+

Donation
+---------+---------+----------+
| id_dona | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |    10    |
|    2    |    2    |    20    |
|    3    |    1    |    30    |
|    4    |    3    |    5     |
+---------+---------+----------+

Beneficiation
+---------+---------+----------+ 
| id_bene | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |   -5     |
|    2    |    2    |   -10    |
|    3    |    1    |   -15    |
+---------+---------+----------+

Table expected
+---------+-------------+----------+
| ID_Prod | Description | Quantity |
+---------+-------------+----------+
|    1    |    tshirt   |    20    |
|    2    |    pants    |    10    |
|    3    |      hat    |    5     |
+---------+-------------+----------+

试着把两者的总和相加

ProdStock
+---------+--------------+
| ID_Prod |  Description |
+---------+--------------+
|    1    |    tshirt    |
|    2    |    pants     |
|    3    |      hat     |
+---------+--------------+

Donation
+---------+---------+----------+
| id_dona | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |    10    |
|    2    |    2    |    20    |
|    3    |    1    |    30    |
|    4    |    3    |    5     |
+---------+---------+----------+

Beneficiation
+---------+---------+----------+ 
| id_bene | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |   -5     |
|    2    |    2    |   -10    |
|    3    |    1    |   -15    |
+---------+---------+----------+

Table expected
+---------+-------------+----------+
| ID_Prod | Description | Quantity |
+---------+-------------+----------+
|    1    |    tshirt   |    20    |
|    2    |    pants    |    10    |
|    3    |      hat    |    5     |
+---------+-------------+----------+

试着把两者的总和相加

ProdStock
+---------+--------------+
| ID_Prod |  Description |
+---------+--------------+
|    1    |    tshirt    |
|    2    |    pants     |
|    3    |      hat     |
+---------+--------------+

Donation
+---------+---------+----------+
| id_dona | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |    10    |
|    2    |    2    |    20    |
|    3    |    1    |    30    |
|    4    |    3    |    5     |
+---------+---------+----------+

Beneficiation
+---------+---------+----------+ 
| id_bene | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |   -5     |
|    2    |    2    |   -10    |
|    3    |    1    |   -15    |
+---------+---------+----------+

Table expected
+---------+-------------+----------+
| ID_Prod | Description | Quantity |
+---------+-------------+----------+
|    1    |    tshirt   |    20    |
|    2    |    pants    |    10    |
|    3    |      hat    |    5     |
+---------+-------------+----------+

试着把两者的总和相加

ProdStock
+---------+--------------+
| ID_Prod |  Description |
+---------+--------------+
|    1    |    tshirt    |
|    2    |    pants     |
|    3    |      hat     |
+---------+--------------+

Donation
+---------+---------+----------+
| id_dona | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |    10    |
|    2    |    2    |    20    |
|    3    |    1    |    30    |
|    4    |    3    |    5     |
+---------+---------+----------+

Beneficiation
+---------+---------+----------+ 
| id_bene | ID_Prod | Quantity |
+---------+---------+----------+
|    1    |    1    |   -5     |
|    2    |    2    |   -10    |
|    3    |    1    |   -15    |
+---------+---------+----------+

Table expected
+---------+-------------+----------+
| ID_Prod | Description | Quantity |
+---------+-------------+----------+
|    1    |    tshirt   |    20    |
|    2    |    pants    |    10    |
|    3    |      hat    |    5     |
+---------+-------------+----------+
像这样的

SELECT  p.ID_Prod,
        Description,
        ISNULL(d.Quantity,0) + ISNULL(b.Quantity,0) AS Quantity
FROM    ProdStock p
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM   Donation 
                         GROUP BY ID_Prod) d ON p.ID_Prod = d.ID_Prod
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM Beneficiation 
                         GROUP BY ID_Prod) b ON p.ID_Prod = b.ID_Prod
像这样的

SELECT  p.ID_Prod,
        Description,
        ISNULL(d.Quantity,0) + ISNULL(b.Quantity,0) AS Quantity
FROM    ProdStock p
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM   Donation 
                         GROUP BY ID_Prod) d ON p.ID_Prod = d.ID_Prod
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM Beneficiation 
                         GROUP BY ID_Prod) b ON p.ID_Prod = b.ID_Prod
像这样的

SELECT  p.ID_Prod,
        Description,
        ISNULL(d.Quantity,0) + ISNULL(b.Quantity,0) AS Quantity
FROM    ProdStock p
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM   Donation 
                         GROUP BY ID_Prod) d ON p.ID_Prod = d.ID_Prod
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM Beneficiation 
                         GROUP BY ID_Prod) b ON p.ID_Prod = b.ID_Prod
像这样的

SELECT  p.ID_Prod,
        Description,
        ISNULL(d.Quantity,0) + ISNULL(b.Quantity,0) AS Quantity
FROM    ProdStock p
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM   Donation 
                         GROUP BY ID_Prod) d ON p.ID_Prod = d.ID_Prod
        LEFT OUTER JOIN (SELECT ID_Prod, 
                                SUM(Quantity) Quantity 
                         FROM Beneficiation 
                         GROUP BY ID_Prod) b ON p.ID_Prod = b.ID_Prod

不能简单地在这些表上使用
JOIN
,因为这里存在
1:N
关系。如果使用
联接
,则会得到行重复和无效结果

相反,您可以使用子查询从两个链接表中引入vale,如下所示:

SELECT  ps.ID_Prod,
                ps.Description,
                SUM(d.Quantity) + SUM(b.Quantity) AS Quantity
FROM ProdStock ps
            INNER JOIN Donation d ON ps.ID_Prod = d.ID_Prod
            INNER JOIN Beneficiation b ON d.ID_Prod = b.ID_Prod
GROUP BY ps.ID_Prod, ps.Description 

不能简单地在这些表上使用
JOIN
,因为这里存在
1:N
关系。如果使用
联接
,则会得到行重复和无效结果

相反,您可以使用子查询从两个链接表中引入vale,如下所示:

SELECT  ps.ID_Prod,
                ps.Description,
                SUM(d.Quantity) + SUM(b.Quantity) AS Quantity
FROM ProdStock ps
            INNER JOIN Donation d ON ps.ID_Prod = d.ID_Prod
            INNER JOIN Beneficiation b ON d.ID_Prod = b.ID_Prod
GROUP BY ps.ID_Prod, ps.Description 

不能简单地在这些表上使用
JOIN
,因为这里存在
1:N
关系。如果使用
联接
,则会得到行重复和无效结果

相反,您可以使用子查询从两个链接表中引入vale,如下所示:

SELECT  ps.ID_Prod,
                ps.Description,
                SUM(d.Quantity) + SUM(b.Quantity) AS Quantity
FROM ProdStock ps
            INNER JOIN Donation d ON ps.ID_Prod = d.ID_Prod
            INNER JOIN Beneficiation b ON d.ID_Prod = b.ID_Prod
GROUP BY ps.ID_Prod, ps.Description 

不能简单地在这些表上使用
JOIN
,因为这里存在
1:N
关系。如果使用
联接
,则会得到行重复和无效结果

相反,您可以使用子查询从两个链接表中引入vale,如下所示:

SELECT  ps.ID_Prod,
                ps.Description,
                SUM(d.Quantity) + SUM(b.Quantity) AS Quantity
FROM ProdStock ps
            INNER JOIN Donation d ON ps.ID_Prod = d.ID_Prod
            INNER JOIN Beneficiation b ON d.ID_Prod = b.ID_Prod
GROUP BY ps.ID_Prod, ps.Description 


这里有一个1:N关系,这样连接两者会导致链接创建的数据无效。调用本机函数“ISNULL”时参数计数不正确,导致出现这种情况,所以您不使用sql server
IFNULL
在MySql中是等效的,您可以用
IFNULL
替换
ISNULL
,如果这是您正在使用的,我使用的是phpmyadmin,很抱歉没有提到这里有一个1:N关系,以这种方式连接这两个链接将导致链接创建的数据无效。调用本机函数“ISNULL”时参数计数不正确,导致出现这种情况,所以您没有使用sql server
IFNULL
在MySql中是等效的,您可以用
IFNULL
替换
ISNULL
,如果这是您正在使用的,我使用的是phpmyadmin,很抱歉没有提到这里有一个1:N关系,以这种方式连接这两个链接将导致链接创建的数据无效。调用本机函数“ISNULL”时参数计数不正确,导致出现这种情况,所以您没有使用sql server
IFNULL
在MySql中是等效的,您可以用
IFNULL
替换
ISNULL
,如果这是您正在使用的,我使用的是phpmyadmin,很抱歉没有提到这里有一个1:N关系,以这种方式连接这两个链接将导致链接创建的数据无效。调用本机函数“ISNULL”时参数计数不正确,导致出现这种情况,所以您没有使用sql server
IFNULL
在MySql中是等效的,您可以用
IFNULL
替换
ISNULL
,如果这是您正在使用的,抱歉没有提及请确保您没有添加NULL,因为
hat
中没有实益记录这正是调用本机函数“ISNULL”时出现的错误参数计数的数量,这让我感到抱歉,已修复。请确保您没有添加NULL,因为
hat
中没有实益记录这正是调用本机函数“ISNULL”时hat参数计数不正确的数量,这让我很抱歉,已修复。请确保您没有添加NULL,因为
hat
中没有实益记录这正是调用本机函数“ISNULL”时hat参数计数不正确的数量,这让我很抱歉,已修复。请确保您没有添加NULL,因为
hat
在实益中没有记录这正是调用本机函数“ISNULL”时hat参数计数不正确时出现的数量,这让我很抱歉,已修复。