Mysql - 从两个不同的查询添加动态列

时间:2017-06-26 19:49:38

标签: mysql

我有两个数据库。

第一个名为ydepotras_estimator的数据库,我有一个名为repair_estimate的表。

mysql> DESC ydepotras_estimator.repair_estimate;
+-------------------------------+---------------------------------+------+-----+-------------------+----------------+
| Field                         | Type                            | Null | Key | Default           | Extra          |
+-------------------------------+---------------------------------+------+-----+-------------------+----------------+
| id                            | int(11)                         | NO   | PRI | NULL              | auto_increment |
| eir_ref                       | varchar(30)                     | YES  |     | NULL              |                |
| tanggal                       | date                            | YES  |     | NULL              |                |
| nama_depot                    | varchar(30)                     | YES  |     | PT. Depo Tras     |                |

现在在名为ydepotras_finance的第二个数据库中,我有两个表与上面的表相关。

关系是

的定义
(`ydepotras_estimator.repair_estimate.id => ydepotras_finance.tableN.repair_estimate_id`)

结构看起来像这样:

mysql> DESC ydepotras_finance.tagihan_cleaning;
+--------------------+---------------+------+-----+---------+----------------+
| Field              | Type          | Null | Key | Default | Extra          |
+--------------------+---------------+------+-----+---------+----------------+
| id                 | int(11)       | NO   | PRI | NULL    | auto_increment |
| repair_estimate_id | int(11)       | NO   | MUL | 0       |                |
| level              | varchar(50)   | YES  |     | NULL    |                |
| bl_number          | char(50)      | YES  |     | NULL    |                |
| cleaning           | decimal(16,2) | YES  |     | NULL    |                |
| manhour            | decimal(16,2) | YES  |     | NULL    |                |
| remarks            | text          | YES  |     | NULL    |                |
| ditagihkan_bulan   | date          | YES  |     | NULL    |                |
+--------------------+---------------+------+-----+---------+----------------+

现在,第二个表:

mysql> DESC ydepotras_finance.tagihan_one_bar;
+--------------------+---------------+------+-----+---------+----------------+
| Field              | Type          | Null | Key | Default | Extra          |
+--------------------+---------------+------+-----+---------+----------------+
| id                 | int(11)       | NO   | PRI | NULL    | auto_increment |
| repair_estimate_id | int(11)       | NO   | MUL | 0       |                |
| level              | varchar(50)   | YES  |     | NULL    |                |
| bl_number          | char(50)      | YES  |     | NULL    |                |
| one_bar            | decimal(16,2) | YES  |     | NULL    |                |
| manhour            | decimal(16,2) | YES  |     | NULL    |                |
| remarks            | text          | YES  |     | NULL    |                |
| ditagihkan_bulan   | date          | YES  |     | NULL    |                |
+--------------------+---------------+------+-----+---------+----------------+
8 rows in set

如您所见,上面两张桌子的结构看起来很相似。 我怎么能基于这些表的SUM(),我的意思是:

    mysql> SELECT cu.id, tc.ditagihkan_bulan, SUM(tc.cleaning) as total_cleaning 
FROM ydepotras_finance.tagihan_cleaning tc

INNER JOIN ydepotras_estimator.repair_estimate  re
ON tc.repair_estimate_id = re.id

INNER JOIN ydepotras_estimator.inspection_report ir
ON re.inspection_id = ir.id

INNER JOIN ydepotras_estimator.customer cu
ON cu.id = ir.customer_id


AND MONTH(tc.ditagihkan_bulan) = '05'
AND YEAR( tc.ditagihkan_bulan)='2017';
+----+------------------+----------------+
| id | ditagihkan_bulan | total_cleaning |
+----+------------------+----------------+
| 58 | 2017-05-01       | 15402.00       |
+----+------------------+----------------+
1 row in set

AND第二个查询是:

mysql> SELECT cu.id, tob.ditagihkan_bulan, SUM(tob.one_bar) as total_onebar
FROM ydepotras_finance.tagihan_one_bar tob

INNER JOIN ydepotras_estimator.repair_estimate  re
ON tob.repair_estimate_id = re.id

INNER JOIN ydepotras_estimator.inspection_report ir
ON re.inspection_id = ir.id

INNER JOIN ydepotras_estimator.customer cu
ON cu.id = ir.customer_id


AND MONTH(tob.ditagihkan_bulan) = '05'
AND YEAR( tob.ditagihkan_bulan)='2017';
+----+------------------+--------------+
| id | ditagihkan_bulan | total_onebar |
+----+------------------+--------------+
| 58 | 2017-05-01       | 1869.20      |
+----+------------------+--------------+
1 row in set

你知道,我需要这样吗

+----+------------------+----------------+--------------+
| id | ditagihkan_bulan | total_cleaning | total_onebar |
+----+------------------+----------------+--------------+
| 58 | 2017-05-01       | 15402.00       |  1869.20     |
+----+------------------+----------------+--------------+

请告知。

ANSWERED

全心全意,

SELECT a.id, a.total_cleaning, b.total_onebar
FROM(
    SELECT cu.id, tc.ditagihkan_bulan, SUM(tc.cleaning) as total_cleaning 
        FROM ydepotras_finance.tagihan_cleaning tc
    INNER JOIN ydepotras_estimator.repair_estimate  re
        ON tc.repair_estimate_id = re.id
    INNER JOIN ydepotras_estimator.inspection_report ir
        ON re.inspection_id = ir.id
    INNER JOIN ydepotras_estimator.customer cu
        ON cu.id = ir.customer_id
    WHERE cu.id = 36
        AND MONTH(tc.ditagihkan_bulan) = '06'
        AND YEAR( tc.ditagihkan_bulan)='2017'
) as A
JOIN (
    SELECT cu.id, tob.ditagihkan_bulan, SUM(tob.one_bar) as total_onebar
        FROM ydepotras_finance.tagihan_one_bar tob
    INNER JOIN ydepotras_estimator.repair_estimate  re
    ON tob.repair_estimate_id = re.id
    INNER JOIN ydepotras_estimator.inspection_report ir
    ON re.inspection_id = ir.id
    INNER JOIN ydepotras_estimator.customer cu
    ON cu.id = ir.customer_id
    WHERE cu.id = 36
    AND MONTH(tob.ditagihkan_bulan) = '06'
    AND YEAR( tob.ditagihkan_bulan)='2017'
) as B

ON A.id = B.id

1 个答案:

答案 0 :(得分:1)

加入两个查询。

SELECT a.id, a.total_cleaning, b.total_onebar
FROM (
    SELECT cu.id, tc.ditagihkan_bulan, SUM(tc.cleaning) as total_cleaning 
    FROM ydepotras_finance.tagihan_cleaning tc

    INNER JOIN ydepotras_estimator.repair_estimate  re
    ON tc.repair_estimate_id = re.id

    INNER JOIN ydepotras_estimator.inspection_report ir
    ON re.inspection_id = ir.id

    INNER JOIN ydepotras_estimator.customer cu
    ON cu.id = ir.customer_id
    GROUP BY cu.id) AS a
JOIN (
    SELECT cu.id, tob.ditagihkan_bulan, SUM(tob.one_bar) as total_onebar
    FROM ydepotras_finance.tagihan_one_bar tob

    INNER JOIN ydepotras_estimator.repair_estimate  re
    ON tob.repair_estimate_id = re.id

    INNER JOIN ydepotras_estimator.inspection_report ir
    ON re.inspection_id = ir.id

    INNER JOIN ydepotras_estimator.customer cu
    ON cu.id = ir.customer_id
    GROUP BY cu.id) AS b
ON a.id = b.id