I am trying to retrieve records from 4 tables and want to use a sub query within a left Join. So basically, i have table a is the table for openings o,id, o.namesdesc
table b is the table of all the projects b.id, b.titledesc,b,status,b.edate
table a_b is the table for all the opening that have changed to projects acts like a junction table for one to many o.id, b.id
table br is the relationship table with other projects b.id b_rel_code (this is also a b.id as in related to which other projects)
I want to get all the openings,check whether they have project codes from the table a_b table get the projects codes relationship with other project codes check the status of the projects and get all the end_dates of the projects
I would really appreciate your help on this. I have been banging my head from ages. I think i am just not seeing it through
SELECT
o.id,
o.namesdesc,
GROUP_CONCAT(ab.pcode) as junctionpcode,
GROUP_CONCAT(br.scode) as proj_link_code,
GROUP_CONCAT(b.status)
FROM table_ab as ab
Left JOIN tablea as o on ab.oid = o.id
Left Join tableb as b on ab.pcode= b.id
LEFT Join tablebr as br on br.b_rel_code=b.id
where b.status='E'
GROUP BY o.id order by junctionpcode
Many Thanks, Ok, Probably i have not explained well. May be sample data should help this
Tablea lists description of openings id,namedesc 1,test desc1 2,test desc2
Tableb list of all the projects
pcode,pname,status,edate
001,test pr1,E,30-10-2017
002,test pr2,E,30-05-2017
003,test pr3,A,30-10-2018
Table br is the relationship table for the projects that are related to other projects
brid pcode rel_pcode
1 001 003
2 003 001
Table a_b which is the junction table between tablea and tableb
abid pcode id
1 001 1
2 002 2
3 003 3
So what i want to achieve is get all the openings(from tablea) that has project codes associated with it(from tablea_b) and get the status and enddates of the projects for only with status E(from table b). But it has needs to consider if that project code has the link to the project code(from table br) which has status and edate.
The output that i am expecting is
a.id,concat(pcode+relcodeany),concat(pstatus,relstatus),concat(pedate,relcodeedate)
1 ,(001,003),(E,A),(30-10-2017,30-10-2018)
I hope i have provided enough info. Sorry for the long post
SELECT o.id,o.namesdesc,GROUP_CONCAT(ab.pcode) as junctionpcode,GROUP_CONCAT(br.scode) as proj_link_code, GROUP_CONCAT(b.status) FROM table_ab as ab Left JOIN tablea as o on ab.oid = o.id Left Join tableb as b on ab.pcode= b.id LEFT Join tablebr as br on br.b_rel_code=b.id where b.status='E' GROUP BY o.id order by junctionpcode
I hope i am making sense – Teju Desai Oct 12 '18 at 08:56