I'm trying to do something I thought would be simple but either I'm loosing it after trying to many different ways and my brain stopped working or this is not 'simple'
I have 2 simple tables:
CREATE TABLE `Orders` (
`Id` int NOT NULL,
`Customer` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
`Value` decimal(9,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
CREATE TABLE `OrderItems` (
`Id` int NOT NULL,
`OrderId` int NOT NULL, // FK to Order (Id)
`Qty` int NOT NULL,
`Price` decimal(9,0) NOT NULL,
`Total` decimal(9,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
I'm trying to select this in a JSON result that looks like this...
{
"Orders": [
{
"Id": 1,
"Customer": "Joe Bloggs",
"Value": 120.00,
"Items": [
{
"Id": 1,
"Qty": 3,
"Price": 20.00,
"Total": 60.00
},
{
"Id": 2,
"Qty": 2,
"Price": 30.00,
"Total": 60.00
}
]
},
{
"Id": 2,
"Customer": "Sam Rockstar",
"Value": 140.00,
"Items": [
{
"Id": 1,
"Qty": 2,
"Price": 35.00,
"Total": 70.00
},
{
"Id": 2,
"Qty": 1,
"Price": 70.00,
"Total": 70.00
}
]
},
{
"Id": 3,
"Customer": "Jack Jones",
"Value": 0.00,
"Items": []
}
]
}
I don't seem to be able to get the cases where the OrderItems is empty that it doesn't show null information :-(
Any pointers will be great - thanks!