In the department table, I have two fields:
documentid, which is INT
jsondocument which is JSON
I executed the following query:
INSERT INTO department VALUES
(1,'{"department":{
"deptid":"d1",
"deptname":"Marketing",
"deptroom":"Room 7",
"deptphone":["465-8541","465-8542","465-8543"],
"employee":[{
"empid":"e1",
"empname":"Mary Jones",
"empphone":"465-8544",
"empemail":["mjones@gmail.com","mjones@company.com"]},
{
"empid":"e2",
"empname":"Tom Robinson",
"empphone":"465-8545",
"empemail":["trobinson@gmail.com","trobinson@company.com"]},
{
"empid":"e3",
"empname":"Olivia Johnson",
"empphone":"465-8546",
"empemail":["ojohnson@gmail.com","ojohnson@company.com"]}
]}} ' );
Using the same query, I added 2 more departments with 3 employees each. Demo on DB Fiddle. I want to return only 1 department and list all the employees so it would look like this:
deptname | employees
"Marketing" | "Mary Jones, Tom Robinson, Olivia Johnson" (location of quotations marks doesn't matter)
But the closest I've been able to figure out is this query, which lists all the departments and only the 1st employee in each:
select
jsondocument->'$.department.deptname' as deptname,
jsondocument->'$.department.employee[0].empname' as employees
from department;
It's homework -- a beginner class and I've studied hard to even get to this point. Any help would be appreciated.