Hi what I’m trying to do is obtain some customer details from one table and also all of their items in another table through one query. The current issue with my subquery is that it will return more than 1 row and cause an error. Is there a way around this or should I use 2 separate queries?
I’d like to use PDO fetchAll() on the query to return their first and last name and a subarray containing all of the items.
So the results would be accessible by $result[‘First Name’], $result[‘First Name’], $result[‘product’][0], $result[‘product’][1], $result[‘product’][3] etc.
Current statement:
SELECT `First Name`, `Last Name`,
(SELECT `items`.`Product Name` from items
inner join customers on `customers`.`Customer No` = `items`.`Customer No`
WHERE `customers`.`Customer No` = '6')
AS product from customers where `Customer No` = '6'
I don’t think it’s possible to return an array within one row, but what you can do is use
group_concatto join the values in to a string which you can then explode later on:The default separator for
group_concatis,which can be quite dangerous when using explode so we useSEPARATOR '$^$'to add a bunch of random characters that are not likely to turn up instead.