StudentType
| ID | Name |
|---|---|
| 1 | Hostel |
| 2 | Home |
Students
| ID | Name | studentTypeId | deletedAt |
|---|---|---|---|
| 1 | First | 1 | null. |
| 2 | Second | 1 | 2022-02-07 |
| 3 | Third | null. | |
| 4 | Fourth | 2 | null |
| 5 | Fifth | 2 | null |
So, I have two tables studentTypes and students. The student table could exist without the studentTypeId.
Using sql I want to be able to get the following json. The student with no id needs to be under unassigned type.
{
"studentTypes": [{
"id": 1,
"name": "Hostel",
"students": [{
"id": 1,
"name": "First",
"studentTypeId": 1
}
]
},
{
"id": 2,
"name": "Home Going",
"students": [{
"id": 1,
"name": "Fourth",
"studentTypeId": 2
},
{
"id": 1,
"name": "Fifth",
"studentTypeId": 2
}
]
},
{
"name": "Unassigned",
"students": [{
"id": 3,
"name": "Third",
"studentTypeId": null
},
{
"id": 1,
"name": "Second",
"studentTypeId": 1
}]
}
]
}
I want the last type as Unassigned which will have students with no studentTypeId as well as which has deletedAt set. I cant figure-out how to get it.