如何在MYSQL中查询多个表

时间:2018-07-09 06:24:25

标签: mysql

假设我的数据库中有一些这样的表。 第一个是DA表,它具有三列,我想基于PROJECT_ID查询其他表。

enter image description here

下一个是WORK表。我想通过特定的da_id计算work_bonus和work_salary的总和。 enter image description here

下一张表是费用表

enter image description here

下一个是产品表

enter image description here

最后一个是汇总表。我想像这张桌子下面那样打印DA。这意味着假设我想基于project_id“ 15”搜索da,因此我将从da表中获得两个da_id,即da_id 1和2。现在工作,费用和产品表都有da_id字段。所以我想从工作,费用和产品表中打印DA摘要。在WORK表中,DA_ID 1有两个记录,即work_id 1和3。我想基于DA_ID总计其WORK_BONUS和WORK_SALARY值,这意味着DA_ID 1的总WORK_BONUS将为40,DA_ID 1的WORK_SALARY将为400。像这样,其余表计算将是相同的。这是我想要的决赛桌。那么,您能帮我在mysql中计算结果吗?谢谢

enter image description here

文本版本-

DA TABLE
-------------------------------------------------
|   DA_ID     |    PROJECT_ID  |      DA_DATE    |
-------------------------------------------------
|    1        |       15        |       8/7/18   |
-------------------------------------------------
|    2        |       15        |       4/7/18   |
-------------------------------------------------
|    3        |       14        |       6/7/18   |
-------------------------------------------------



WORK TABLE
--------------------------------------------------------------------
|   WORK_ID   |     DA_ID      |    WORK_BONUS  |      WORK_SALARY   |
--------------------------------------------------------------------
|    1        |       1        |       10       |           100      |
--------------------------------------------------------------------
|    2        |       2        |       20       |          200       |
-------------------------------------------------------------------
|    3        |       1        |       30       |          200       |
--------------------------------------------------------------------



EXPENSE TABLE
-----------------------------------------------------
|   EXP_ID    |    DA_ID        |      EXP_TOTAL    |
-----------------------------------------------------
|    1        |       1         |       10          |
-----------------------------------------------------
|    2        |       2         |       20          |
-----------------------------------------------------
|    3        |       1         |       30          |
-----------------------------------------------------



PRODUCT TABLE
-------------------------------------------------------------
|   PRODUCT_ID    |    DA_ID        |      PRODUCT_TOTAL    |
-------------------------------------------------------------
|       1         |       1         |       5               |
-------------------------------------------------------------
|       2         |       2         |       10              |
-------------------------------------------------------------
|       3         |       1         |       15              |
-------------------------------------------------------------


SUMMARY TABLE
----------------------------------------------------------------------------------------
|   SUMMARY_ID    |   DA_ID   |   TOTAL_BONUS  |   TOTAL_EXPENSE   |   TOTAL_PRODUCT   |
----------------------------------------------------------------------------------------
|       1         |     1     |        40      |        400        |         20        |
----------------------------------------------------------------------------------------
|       2         |     2     |        20      |        200        |         10        |
----------------------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:1)

此查询将为您提供帮助:

SELECT * FROM 
(
    SELECT sum(WORK_BONUS) AS TOTAL_BONUS, 
           sum(WORK_SALARY) AS TOTAL_SALARY 
    FROM WORK WHERE DA_ID = ?)
) t1,
(
    SELECT sum(EXP_TOTAL) as TOTAL_EXPENSE 
    FROM EXPENSE WHERE DA_ID = ?
) t2, 
(
    SELECT sum(PRODUCT_TOTAL) AS TOTAL_PRODUCT 
    FROM PRODUCT WHERE DA_ID = ?
);

答案 1 :(得分:0)

有两种方法可以使用查询来实现。可能有更合适的方法

方法1

SELECT D.DA_ID,
    (SELECT SUM(WORK_BONUS) FROM `WORK` W WHERE W.DA_ID = D.DA_ID) as TOTAL_BONUS,
    (SELECT SUM(WORK_SALARY) FROM `WORK` W WHERE W.DA_ID = D.DA_ID) as TOTAL_SALARY,
    (SELECT SUM(EXP_TOTAL) FROM EXPENSE E WHERE E.DA_ID = D.DA_ID) as TOTAL_EXPENSE,
    (SELECT SUM(PRODUCT_TOTAL) FROM PRODUCT P WHERE P.DA_ID = D.DA_ID) as TOTAL_PRODUCT
FROM DA as D left join WORK as W on D.DA_ID = W.DA_ID
WHERE D.PROJECT_ID = 15
GROUP BY D.DA_ID;

方法2

SELECT D.DA_ID, SUM(WORK_BONUS) as TOTAL_BONUS, SUM(WORK_SALARY) as TOTAL_SALARY, 
    (SELECT SUM(EXP_TOTAL) FROM EXPENSE E WHERE E.DA_ID = D.DA_ID) as TOTAL_EXPENSE,
    (SELECT SUM(PRODUCT_TOTAL) FROM PRODUCT P WHERE P.DA_ID = D.DA_ID) as TOTAL_PRODUCT
FROM DA as D left join WORK as W on D.DA_ID = W.DA_ID
WHERE D.PROJECT_ID = 15
GROUP BY D.DA_ID;