I need to join two tables in MariaDB in a specific way. For each project in table A I need to add one column for objective 1 and one column for objective 2. The value of objective 1 and objective 2 is the value of the field is_core associated with that objective in the table B in the highest phase as in the example.
In my real problem there will be more objectives but for simplicity I have used only 2 here.
Can anyone help with this? I am not very experienced with DB and my head is burning.
Table A
id | name |
---|---|
1 | project A |
2 | project B |
3 | project C |
Table B
id | project_id | objective | is_core | phase |
---|---|---|---|---|
1 | 1 | objective1 | 0 | 0 |
2 | 1 | objective1 | 0 | 1 |
3 | 1 | objective1 | 1 | 2 |
4 | 1 | objective2 | 0 | 0 |
5 | 1 | objective2 | 0 | 1 |
6 | 1 | objective2 | 0 | 2 |
7 | 2 | objective1 | 1 | 0 |
8 | 2 | objective2 | 0 | 0 |
9 | 3 | objective1 | 0 | 0 |
10 | 3 | objective2 | 0 | 0 |
TableA.id links to TableB.projectId
Desired result:
project_id | name | objective1 | objective2 |
---|---|---|---|
1 | project A | 1 | 0 |
2 | project B | 1 | 0 |
3 | project B | 0 | 0 |
Many thanks!