sql >> Database teknologi >  >> RDS >> Mysql

MYSQL LEFT JOIN MED GROUP BY

Du kan bare slå de 2 forespørgsler sammen som et par undervalg.

Noget som:-

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

Kan dog ikke rigtig give dig mere, da din første eksempelforespørgsel ikke ser ud til at bringe de detaljer tilbage, du siger (bringer kun 3 kolonner tilbage).

EDIT - Med de rettede forespørgsler

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. CONVERT() fra dato/tid til strengeksempler i SQL Server

  2. LPAD i SQL Server 2008

  3. Oprettelse af et sikkert login-script i PHP og MySQL uden HTTPS

  4. SÆT NAVNE utf8 i MySQL?