MySQL复杂查询-使用UNION ALL和内部JOIN在多个数据库上求和

时间:2019-02-26 12:21:06

标签: mysql sql join sum

我想使用UNION ALL和内部JOIN在多个数据库上计算总和。 MySQL用户具有访问所有相关数据库的权限。

这是到目前为止我的SQL查询代码:

SELECT
    SUM(summen.OP1OPVerfahren = "0") AS "Keine Operation durchgeführt",
    SUM(summen.OP1OPVerfahren = "1") AS "Bioenterics Intragastric Ballon (BIB)",
    SUM(summen.OP1OPVerfahren = "2") AS "Gastric Banding",
    SUM(summen.OP1OPVerfahren = "3") AS "Roux-en-Y Gastric Bypass",
    SUM(summen.OP1OPVerfahren LIKE "%") AS "Summe"

FROM
(
    SELECT
        SUM(op.OP1OPVerfahren = "0") AS "Keine Operation durchgeführt",
        SUM(op.OP1OPVerfahren = "1") AS "Bioenterics Intragastric Ballon (BIB)",
        SUM(op.OP1OPVerfahren = "2") AS "Gastric Banding",
        SUM(op.OP1OPVerfahren = "3") AS "Roux-en-Y Gastric Bypass",
        SUM(op.OP1OPVerfahren LIKE "%") AS "Summe"
        FROM ods01.dat_patient p
        LEFT OUTER JOIN ods01.dat_optherapie op ON op.PatID = p.ID
        WHERE Testzwecke = 0
        AND p.ID = op.PatID  -- possibly redundant
        AND NOT EXISTS (SELECT 1
                        FROM ods01.dat_optherapie op2
                        WHERE op2.PatID = p.ID AND op2.revision > op.revision)
        GROUP BY OP1OPVerfahren

    UNION ALL

    SELECT
        SUM(op.OP1OPVerfahren = "0") AS "Keine Operation durchgeführt",
        SUM(op.OP1OPVerfahren = "1") AS "Bioenterics Intragastric Ballon (BIB)",
        SUM(op.OP1OPVerfahren = "2") AS "Gastric Banding",
        SUM(op.OP1OPVerfahren = "3") AS "Roux-en-Y Gastric Bypass",
        SUM(op.OP1OPVerfahren LIKE "%") AS "Summe"
        FROM ods02.dat_patient p
        LEFT OUTER JOIN ods02.dat_optherapie op ON op.PatID = p.ID
        WHERE Testzwecke = 0
        AND p.ID = op.PatID  -- possibly redundant
        AND NOT EXISTS (SELECT 1
                        FROM ods02.dat_optherapie op2
                        WHERE op2.PatID = p.ID AND op2.revision > op.revision)
        GROUP BY OP1OPVerfahren
) summen

GROUP BY OP1OPVerfahren

无论我在前5行中做什么-离开“ summen”,离开“ summen”。或与“ op”交换。 -我收到错误消息:

  

SQL错误(1054):“字段”中的未知列“ summen.OP1OPVerfahren”   列表”

...或...

  

SQL错误(1054):“字段列表”中的未知列“ OP1OPVerfahren”

...或...

  

SQL错误(1054):“字段列表”中的未知列“ op.OP1OPVerfahren”

我的逻辑错误在哪里?

我在这里看到了所有与此有关的其他参考,但是没有找到任何有关JOINed表集成的话题(这不应该成为问题)。

有人知道我需要更改什么吗?

2 个答案:

答案 0 :(得分:2)

您需要在两个内部查询的选择列表中添加OP1OPVerfahren-

SELECT
    SUM(OP1OPVerfahren = "0") AS "Keine Operation durchgeführt",
    SUM(OP1OPVerfahren = "1") AS "Bioenterics Intragastric Ballon (BIB)",
    SUM(OP1OPVerfahren = "2") AS "Gastric Banding",
    SUM(OP1OPVerfahren = "3") AS "Roux-en-Y Gastric Bypass",
    SUM(OP1OPVerfahren LIKE "%") AS "Summe"

FROM
(
    SELECT OP1OPVerfahren,
        SUM(op.OP1OPVerfahren = "0") AS "Keine Operation durchgeführt",
        SUM(op.OP1OPVerfahren = "1") AS "Bioenterics Intragastric Ballon (BIB)",
        SUM(op.OP1OPVerfahren = "2") AS "Gastric Banding",
        SUM(op.OP1OPVerfahren = "3") AS "Roux-en-Y Gastric Bypass",
        SUM(op.OP1OPVerfahren LIKE "%") AS "Summe"
        FROM ods01.dat_patient p
        LEFT OUTER JOIN ods01.dat_optherapie op ON op.PatID = p.ID
        WHERE Testzwecke = 0
        AND p.ID = op.PatID  -- possibly redundant
        AND NOT EXISTS (SELECT 1
                        FROM ods01.dat_optherapie op2
                        WHERE op2.PatID = p.ID AND op2.revision > op.revision)
        GROUP BY OP1OPVerfahren

    UNION ALL

    SELECT OP1OPVerfahren,
        SUM(op.OP1OPVerfahren = "0") AS "Keine Operation durchgeführt",
        SUM(op.OP1OPVerfahren = "1") AS "Bioenterics Intragastric Ballon (BIB)",
        SUM(op.OP1OPVerfahren = "2") AS "Gastric Banding",
        SUM(op.OP1OPVerfahren = "3") AS "Roux-en-Y Gastric Bypass",
        SUM(op.OP1OPVerfahren LIKE "%") AS "Summe"
        FROM ods02.dat_patient p
        LEFT OUTER JOIN ods02.dat_optherapie op ON op.PatID = p.ID
        WHERE Testzwecke = 0
        AND p.ID = op.PatID  -- possibly redundant
        AND NOT EXISTS (SELECT 1
                        FROM ods02.dat_optherapie op2
                        WHERE op2.PatID = p.ID AND op2.revision > op.revision)
        GROUP BY OP1OPVerfahren
) summen
GROUP BY OP1OPVerfahren

答案 1 :(得分:2)

您已经分配了别名,因此可以引用内部列名

connect(ui->print_books_btn, SIGNAL(released()), this, SLOT(printBookList));

void MainWindow::printBookList()
{
    printList(bookList);
}