:)我有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
第一个查询返回我业工人activites分组,例如总工作时间:
u.login a.article p.p_article tottime
Ivan Teambuilding 1 3,45
Julie Social_work 2 5,67
第二个查询返回我公司通过activites分组,例如总工作时间:
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个查询一起作为一对子查询的。
就像是:-
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
最简单的是使用子查询(尽管他们一般不太有效,但这些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 ?? */