MYSQL LEFT JOIN with GROUP BY

后端 未结 2 705
眼角桃花
眼角桃花 2021-02-04 09:57

:) I have 2 queries, and I need to join them, I need to compare the working time of employee depending on activity with total working time of company in the same activity in def

2条回答
  •  星月不相逢
    2021-02-04 10:38

    You can just join the 2 queries together as a pair of subselects.

    Something like:-

    SELECT Sub1.a, Sub1.b, Sub2.c
    FROM (SELECT a, b FROM z) Sub1
    INNER JOIN (SELECT a, c FROM y) Sub2
    ON Sub1.a = Sub2.a
    

    However can't really give you more as you first example query doesn't seem to bring back the details you say (only brings back 3 columns).

    EDIT - With the corrected queries

    SELECT Sub1.login AS User_name, Sub1.article AS Activity, Sub1.p_article AS `Activity id`, Sub1.tottime AS `Totaltime(worker)`, Sub2.tottime AS `Totaltime(company)`
    FROM (SELECT u.login,a.article, p.p_article, (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS tottime
    FROM pos p
    INNER JOIN users u ON u.login = p.p_login 
    INNER JOIN articles a ON p.p_article = a.id
    WHERE REPLACE( u.login, '.', '_' ) = 'users_name'
    AND p.p_datum >= '2013-04-09'
    AND p.p_datum <= '2013-04-16'
    GROUP BY a.article) Sub1
    INNER JOIN 
    (SELECT a.article, p.p_article, (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS tottime
    FROM pos p
    INNER JOIN articles a ON p.p_article = a.id
    WHERE p.p_datum >= '2013-04-09'
    AND p.p_datum <= '2013-04-16'
    GROUP BY a.article) Sub2
    ON Sub1.p_article = Sub2.p_article
    

提交回复
热议问题