使用GROUP BY进行MYSQL LEFT JOIN

时间:2013-04-23 07:57:06

标签: mysql group-by sum left-join

:) 我有2个查询,我需要加入他们,我需要根据活动将员工的工作时间与定义期间同一活动中公司的总工作时间进行比较

第一个查询是:

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,users u, articles a
WHERE u.login = p.p_login
AND REPLACE( u.login, '.', '_' ) = 'users_name'
AND p.p_datum >= '2013-04-09'
AND p.p_datum <= '2013-04-16'
AND p.p_article = a.id
GROUP BY a.article

我的第二个问题是:

SELECT a.article, 
       p.p_article, 
       (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS tottime
FROM pos p, articles a
WHERE p.p_datum >= '2013-04-09'
AND p.p_datum <= '2013-04-16'
AND p.p_article = a.id
GROUP BY a.article

第一个查询返回按活动分组的WORKER总工作时间,例如:

u.login    a.article     p.p_article  tottime
Ivan       Teambuilding    1          3,45
Julie      Social_work     2          5,67

第二个查询返回按活动分组的COMPANY的总工作时间,例如:

a.article     p.p_article  tottime
Teambuilding    1         150
Social_work     2         260

我希望有类似的内容,因此我可以将每次活动的工作人员总时间与特定时期内每项活动的公司工作时间总时间进行比较:

u.login    a.article     p.p_article  tottime(worker)  tottime(company) 
Ivan       Teambuilding    1          3,45              150  
Julie      Social_work     2          5,67              260

如果是NULL值,我想使用LEFT JOIN。我正在寻找解决方案3个小时,我尝试的一切都不起作用,所以任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:9)

您可以将两个查询一起作为一对子选择加入。

类似的东西: -

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

然而,不能真正给你更多,因为你的第一个例子查询似乎没有带回你说的细节(只带回3列)。

编辑 - 使用更正的查询

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

答案 1 :(得分:3)

最简单的方法是使用子查询(虽然它们通常效率不高,但那些GROUP BY可能会使其他解决方案变得困难。)

这样的事情应该这样做:

SELECT a.*, b.tottime AS 'Total time (company)'
FROM
    (SELECT u.login, a.article, p.p_article, (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS 'Total time (worker)'
    FROM pos p, users u, articles a
    WHERE u.login = p.p_login
    AND REPLACE( u.login, '.', '_' ) = 'users_name'
    AND p.p_datum >= '2013-04-09'
    AND p.p_datum <= '2013-04-16'
    AND p.p_article = a.id
    GROUP BY a.article) a
LEFT JOIN
    (SELECT a.article, p.p_article, (SUM(p.p_going) + SUM(p.p_leaving) + SUM(p.p_working)) AS tottime
    FROM pos p, articles a
    WHERE p.p_datum >= '2013-04-09'
    AND p.p_datum <= '2013-04-16'
    AND p.p_article = a.id
    GROUP BY a.article) b
ON a.article = b.article /* AND a.p_article = b.p_article ?? */