UNION里面的UNION和JOIN表从不同的SELECT到一个表

时间:2017-09-21 08:54:47

标签: mysql sql union

我有这样的查询

这是我的第一个查询。

SELECT 
            id, 
            COUNT(id) ct_id, 
            SUM(kl) sum_kl, 
            SUM(CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, 
            SUM(CASE WHEN tgl_kondisi <= "2017-09-14" THEN kl ELSE 0 end ) as sum_kl2 
        FROM (
            SELECT 
                id_kondisi as id, 
                tgl_kondisi,
                nilai_potensi_kerugian AS kl 
            FROM laporan_kondisi 
            UNION  
            SELECT 
                id_sub_kondisi, 
                tgl_kondisi, 
                nilai_potensi_kerugian 
            FROM laporan_kondisi 
        ) merged_table  GROUP BY id ORDER BY id;

结果

+------+-------+----------+------+----------+
| id   | ct_id | sum_kl   | tot  | sum_kl2  |
+------+-------+----------+------+----------+
| 01   |     3 | 20000000 |    3 | 20000000 |
| 0101 |     2 |  9000000 |    2 |  9000000 |
| 0102 |     2 | 11000000 |    2 | 11000000 |
| 02   |     1 |        0 |    0 |        0 |
| 0201 |     1 |        0 |    0 |        0 |
| 0202 |     1 |        0 |    0 |        0 |
| 0203 |     1 |        0 |    0 |        0 |
| 03   |     2 |  4000000 |    2 |  4000000 |
| 0301 |     1 |        0 |    1 |        0 |
| 0302 |     2 |  4000000 |    2 |  4000000 |
| 0303 |     1 |        0 |    1 |        0 |
+------+-------+----------+------+----------+

第二个查询

SELECT  
        id,
        SUM(CASE WHEN tgl_tindak_lanjut="2017-09-14" THEN 1 ELSE 0 end ) as count_all_09,
        SUM( CASE WHEN tgl_tindak_lanjut="2017-09-13" THEN 1 ELSE 0 END) as count_09_13 
    FROM ( 
        SELECT 
            a.id_kondisi as id, 
            d.tgl_tindak_lanjut as tgl_tindak_lanjut 
        FROM
            laporan_kondisi a 
        LEFT OUTER JOIN  
            laporan_sebab b 
        ON 
            a.id = b.id_laporan_kondisi 
        LEFT OUTER JOIN  
            laporan_rekomendasi c 
        ON 
            b.id = c.id_laporan_sebab 
        LEFT OUTER JOIN 
            laporan_tindak_lanjut d 
        ON 
            c.id = d.id_laporan_rekomendasi  
        UNION  
        SELECT 
            a2.id_sub_kondisi,
            d2.tgl_tindak_lanjut as tgl_tindak_lanjut 
        FROM
            laporan_kondisi a2 
        LEFT OUTER JOIN 
            laporan_sebab b2 
        ON  
            a2.id = b2.id_laporan_kondisi 
        LEFT OUTER JOIN 
            laporan_rekomendasi c2 
        ON 
            b2.id = c2.id_laporan_sebab 
        LEFT OUTER join  
            laporan_tindak_lanjut d2 
        ON 
            c2.id = d2.id_laporan_rekomendasi  
    ) merged_table GROUP BY id

结果

+------+--------------+-------------+
| id   | count_all_09 | count_09_13 |
+------+--------------+-------------+
| 01   |            0 |           1 |
| 0101 |            0 |           1 |
| 0102 |            0 |           0 |
| 02   |            0 |           0 |
| 0201 |            0 |           0 |
| 0202 |            0 |           0 |
| 0203 |            0 |           0 |
| 03   |            0 |           0 |
| 0301 |            0 |           0 |
| 0302 |            0 |           0 |
| 0303 |            0 |           0 |
+------+--------------+-------------+

我想UNION第一次和第二次查询,所以表格看起来像这样

+------+-------+----------+------+----------+--------------+-------------+
| id   | ct_id | sum_kl   | tot  | sum_kl2  | count_all_09 | count_09_13 |
+------+-------+----------+------+----------+--------------+-------------+
| 01   |     3 | 20000000 |    3 | 20000000 |     0        |       1     |
| 0101 |     2 |  9000000 |    2 |  9000000 |     0        |       1     |
| 0102 |     2 | 11000000 |    2 | 11000000 |     0        |       0     |
| 02   |     1 |        0 |    0 |        0 |     0        |       0     |
| 0201 |     1 |        0 |    0 |        0 |     0        |       0     |
| 0202 |     1 |        0 |    0 |        0 |     0        |       0     |
| 0203 |     1 |        0 |    0 |        0 |     0        |       0     |
| 03   |     2 |  4000000 |    2 |  4000000 |     0        |       0     |
| 0301 |     1 |        0 |    1 |        0 |     0        |       0     |
| 0302 |     2 |  4000000 |    2 |  4000000 |     0        |       0     |
| 0303 |     1 |        0 |    1 |        0 |     0        |       0     |
+------+-------+----------+------+----------+--------------+-------------+

我的查询是这样的。

SELECT 
        id, 
        COUNT(id) ct_id, 
        SUM(kl) sum_kl, 
        SUM(CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, 
        SUM(CASE WHEN tgl_kondisi <= "2017-09-14" THEN kl ELSE 0 end ) as sum_kl2 
    FROM (
        SELECT 
            id_kondisi as id, 
            tgl_kondisi,
            nilai_potensi_kerugian AS kl 
        FROM laporan_kondisi 
        UNION  
        SELECT 
            id_sub_kondisi, 
            tgl_kondisi, 
            nilai_potensi_kerugian 
        FROM laporan_kondisi 
    ) merged_table 

UNION ALL

SELECT  
    id,
    SUM(CASE WHEN tgl_tindak_lanjut="2017-09-14" THEN 1 ELSE 0 end ) as count_all_09,
    SUM( CASE WHEN tgl_tindak_lanjut="2017-09-13" THEN 1 ELSE 0 END) as count_09_13 
FROM ( 
    SELECT 
        a.id_kondisi as id, 
        d.tgl_tindak_lanjut as tgl_tindak_lanjut 
    FROM
        laporan_kondisi a 
    LEFT OUTER JOIN  
        laporan_sebab b 
    ON 
        a.id = b.id_laporan_kondisi 
    LEFT OUTER JOIN  
        laporan_rekomendasi c 
    ON 
        b.id = c.id_laporan_sebab 
    LEFT OUTER JOIN 
        laporan_tindak_lanjut d 
    ON 
        c.id = d.id_laporan_rekomendasi  
    UNION  
    SELECT 
        a2.id_sub_kondisi,
        d2.tgl_tindak_lanjut as tgl_tindak_lanjut 
    FROM
        laporan_kondisi a2 
    LEFT OUTER JOIN 
        laporan_sebab b2 
    ON  
        a2.id = b2.id_laporan_kondisi 
    LEFT OUTER JOIN 
        laporan_rekomendasi c2 
    ON 
        b2.id = c2.id_laporan_sebab 
    LEFT OUTER join  
        laporan_tindak_lanjut d2 
    ON 
        c2.id = d2.id_laporan_rekomendasi  
) merged_table GROUP BY id

我希望UNION ALL在我的FROM中有UNION,而在我的UNION中有LEFT OUTER JOIN,当我尝试此查询时我收到如下错误

  

使用的SELECT语句具有不同的列数

我的代码有什么问题吗?或者我使用的查询无法完成? 我想这不需要SQL小提琴,很可能有经验的人会马上看到错误。

从JNevill Query,我得到了一个结果

+------+-------+----------+------+----------+----+--------------+-------------+
| id   | ct_id | sum_kl   | tot  | sum_kl2  | id | count_all_09 | count_09_13 |
+------+-------+----------+------+----------+----+--------------+-------------+
| 01   |    17 | 48000000 |   13 | 48000000 | 01 |            0 |           1 |
+------+-------+----------+------+----------+----+--------------+-------------+
1 row in set (0.00 sec)

1 个答案:

答案 0 :(得分:1)

而不是Union您正在寻找JoinUnion将两个查询的结果叠加在一起,其中JOIN并排放置两个查询(或表格或视图)的结果JOIN ON SELECT * FROM ( SELECT id, COUNT(id) ct_id, SUM(kl) sum_kl, SUM(CASE WHEN tgl_kondisi="2017-09-13" THEN 1 ELSE 0 END) as tot, SUM(CASE WHEN tgl_kondisi <= "2017-09-14" THEN kl ELSE 0 end ) as sum_kl2 FROM ( SELECT id_kondisi as id, tgl_kondisi, nilai_potensi_kerugian AS kl FROM laporan_kondisi UNION SELECT id_sub_kondisi, tgl_kondisi, nilai_potensi_kerugian FROM laporan_kondisi ) merged_table ) as t1 JOIN ( SELECT id, SUM(CASE WHEN tgl_tindak_lanjut="2017-09-14" THEN 1 ELSE 0 end ) as count_all_09, SUM( CASE WHEN tgl_tindak_lanjut="2017-09-13" THEN 1 ELSE 0 END) as count_09_13 FROM ( SELECT a.id_kondisi as id, d.tgl_tindak_lanjut as tgl_tindak_lanjut FROM laporan_kondisi a LEFT OUTER JOIN laporan_sebab b ON a.id = b.id_laporan_kondisi LEFT OUTER JOIN lap oran_rekomendasi c ON b.id = c.id_laporan_sebab LEFT OUTER JOIN laporan_tindak_lanjut d ON c.id = d.id_laporan_rekomendasi UNION SELECT a2.id_sub_kondisi, d2.tgl_tindak_lanjut as tgl_tindak_lanjut FROM laporan_kondisi a2 LEFT OUTER JOIN laporan_sebab b2 ON a2.id = b2.id_laporan_kondisi LEFT OUTER JOIN laporan_rekomendasi c2 ON b2.id = c2.id_laporan_sebab LEFT OUTER join laporan_tindak_lanjut d2 ON c2.id = d2.id_laporan_rekomendasi ) merged_table GROUP BY id ) as t2 ON t1.id = t2.id 共同的关键。

以下内容适用于您

body {
    background-color:pink;
}

.row-centered {
    display:block;
    text-align:center;
}

.col-centered {
    display:inline-block;
    float:none;
    /* reset the text-align */
    text-align:left;
    /* inline-block space fix */
}

 .container {
    overflow: hidden;
    background-color: yellow;
}

.col-sm-6 {
    height: 100px;
    max-width:100px;
    background-color: blue;
    padding: 10px;
    margin: 1%;
}

查看w3schools example以获得有关SQL连接的快速高级入门。