Thursday, March 17, 2022

[FIXED] MySql join two lines in the same lines

Issue

I try to join two tables in the same line. I try this

SELECT p.*
     , t.joursPrevus joursInge 
  FROM projet p
  JOIN temps t
    ON t.projet_id = p.idProjet 
 WHERE t.role_id = 1
 UNION
SELECT p.*
     , t.joursPrevus joursTech 
  FROM projet p 
  JOIN temps t
    ON t.projet_id = p.idProjet 
 WHERE t.role_id = 2

but I obtain this my table but I want to union this table : table 1 and table 2 with the same idProjet on the same line and role_id: 1 is joursInge and role_id: 2 is joursTech

i try to have a row like:
idProjet - nom - client - achatsPrevus - achatsRestants - dateDebut - dateFin - fini - joursPrevus(role_id:1) - joursPrevus(role_id:2)

If something has had the solution to do what I want, because I'm stuck !


Solution

Probably the simplest approach is to join twice:

SELECT p.*, t1.joursPrevus AS joursInge, t2.joursPrevus AS joursTech
FROM projet p
INNER JOIN temps t1 ON t1.projet_id = p.idProjet  AND t1.role_id = 1
INNER JOIN temps t2 ON t2.projet_id = p.idProjet  AND t2.role_id = 2

This assumes no missing roles in table temps for each and every project - otherwise, you can use left join instead.



Answered By - GMB

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.