PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 17, 2022

[FIXED] MySql join two lines in the same lines

 March 17, 2022     join, mysql, phpmyadmin, select, sql     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing