Mysql 如何从SQL中的3个以上表求和

Mysql 如何从SQL中的3个以上表求和,mysql,Mysql,我有四张桌子 table : user id_tkn | nm_user | ----------------------------------------- 1 | belva | 2 | nanda | 3 | maya | ----------------------------------------- table : maintenance id_mntnc|id_tkn |

我有四张桌子

table : user

id_tkn  |   nm_user     |
-----------------------------------------
    1   |   belva       |
    2   |   nanda       |
    3   |   maya        |
-----------------------------------------

table : maintenance

id_mntnc|id_tkn |   sts |   date    |
-----------------------------------------------------------------
    1   |   2   |   1   |2016-03-03 |
    2   |   2   |   2   |2016-03-03 |
    3   |   1   |   1   |2016-03-03 |
    4   |   2   |   0   |2016-03-03 |
    5   |   2   |   1   |2016-03-03 |
-----------------------------------------------------------------

table : Installasi

id_istlsi|id_tkn|   sts |   date    |
-----------------------------------------------------------------
    1   |   2   |   1   |2016-03-03 |
    2   |   1   |   1   |2016-03-03 |
    3   |   1   |   1   |2016-03-03 |
    4   |   2   |   1   |2016-03-03 |
    5   |   3   |   1   |2016-03-03 |
-----------------------------------------------------------------

table : visit

id_vst  |id_tkn |   sts |   date    |
-----------------------------------------------------------------
    1   |   2   |   1   |2016-03-03 |
    2   |   2   |   0   |2016-03-03 |
    3   |   1   |   1   |2016-03-03 |
-----------------------------------------------------------------
有关“sts”列的信息 0->挂起 1->成功 2->失败。 从上表中,我想通过statussts(其中id_tkn=2)相加,结果如下表所示,如何生成SQL命令以生成下表

id_tkn  |   nm_usr  | maintenance_suc | maintenance_fail | installasi_suc| installasi_fail | visit_suc| visit_fail
-----------------------------------------------------------------------------------------------------------------------
   2    |   nanda   |      2           |       1         |     2         |     0           |     1    |  0

这应该可以正常工作,但可能需要优化:

select 
    u.id_tkn, 
    u.nm_user,
    sum ( case when stat.sts = 1 and stat.typ = 'm' then 1 else 0 end ) as maintenance_suc,
    sum ( case when stat.sts = 2 and stat.typ = 'm' then 1 else 0 end ) as maintenance_fail,
    sum ( case when stat.sts = 1 and stat.typ = 'i' then 1 else 0 end ) as installasi_suc,
    sum ( case when stat.sts = 2 and stat.typ = 'i' then 1 else 0 end ) as installasi_fail,
    sum ( case when stat.sts = 1 and stat.typ = 'v' then 1 else 0 end ) as visit_suc,
    sum ( case when stat.sts = 2 and stat.typ = 'v' then 1 else 0 end ) as visit_fail
from
user u
left join 
(
    select sts, 'm', id_tkn
    from 
    maintenance
    union all
    select sts, 'i', id_tkn
    from 
    Installasi
    union all
    select sts, 'v', id_tkn
    from 
    visit
) stat on u.id_tkn = stat.id_tkn
where u.id_tkn = 2
group by u.id_tkn, u.nm_user

这应该可以正常工作,但可能需要优化:

select 
    u.id_tkn, 
    u.nm_user,
    sum ( case when stat.sts = 1 and stat.typ = 'm' then 1 else 0 end ) as maintenance_suc,
    sum ( case when stat.sts = 2 and stat.typ = 'm' then 1 else 0 end ) as maintenance_fail,
    sum ( case when stat.sts = 1 and stat.typ = 'i' then 1 else 0 end ) as installasi_suc,
    sum ( case when stat.sts = 2 and stat.typ = 'i' then 1 else 0 end ) as installasi_fail,
    sum ( case when stat.sts = 1 and stat.typ = 'v' then 1 else 0 end ) as visit_suc,
    sum ( case when stat.sts = 2 and stat.typ = 'v' then 1 else 0 end ) as visit_fail
from
user u
left join 
(
    select sts, 'm', id_tkn
    from 
    maintenance
    union all
    select sts, 'i', id_tkn
    from 
    Installasi
    union all
    select sts, 'v', id_tkn
    from 
    visit
) stat on u.id_tkn = stat.id_tkn
where u.id_tkn = 2
group by u.id_tkn, u.nm_user

我尝试过,但结果不匹配。维护suc=8,维护失败=0,安装失败=12,安装失败=0,访问失败=6,访问失败=0。我补充说,当在case和stssorry之间我无法运行脚本时,我在with.1064附近得到无法识别的语句类型-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解在第1行中使用“stat sts,typ,id_tkn as select sts,'m',id_tkn from@main”的正确语法。对不起,我给出的标题不对,我使用的是MySQL,MySQL中是否不支持with命令?我在运行脚本1064时将'with'表达式替换为SUBQUERYGETING error-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解在第11I行的@user u left join select sts,'m',id\u tkn from@maintenance'附近使用的正确语法,我已经尝试过,但结果不匹配。maintenance\u suc=8,maintenance\u fail=0,installasi\u suc=12,installasi\u fail=0,visit\u suc=6,visit\u fail=0。我补充说,当在case和stssorry之间我无法运行脚本时,我在with.1064附近得到无法识别的语句类型-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解在第1行中使用“stat sts,typ,id_tkn as select sts,'m',id_tkn from@main”的正确语法。对不起,我给出的标题不对,我使用的是MySQL,MySQL中是否不支持with命令?我在运行脚本1064时将'with'表达式替换为SUBQUERYGETING error-您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解第11行“@user u left join select sts,'m',id_tkn from@maintenance”附近使用的正确语法