MySQL 5.5.36 Apache 2.2.15 CentOS 6.6 PHP 5.4.31
Sorry for the terrible title, but here is what I am looking for help with; I have 2 tables, each order has a single row and the order_items has multiple rows.
order
id | date | customer | status
order_items
id | order_id | item_id | item_name
My current code is cycling through the open orders then doing a secondary db call to grab the items and place them into a single row of an html table like this:
order_id | date | customer | status | item_name
1 | 2014-11-20 | 100233 | open | widget a (item id 0004)
| widget a (item id 0004)
2 | 2014-11-21 | 103327 | open | widget c (item id 0005)
| widget d (item id 0006)
The desired end result is exactly as shown in the above table, but I would like to get the entire thing within a singular query if possible, instead of having to do multiple queries per order displayed.
Now, after googling for a while, I haven't been able to find anything that really is like what I am trying to do. I looked at GROUP_CONCAT, but it only concatenates columns within a single table it looks like. I want to concatenate columns from another table while also grabbing all the rows found.
My attempted SQL is something like this:
select
o.id, o.date, o.customer, o.status, oi.id,
(select concat(item_name, ' (item id ', item_id, ')') from order_items where order_id = o.id) as 'item_list'
from order o inner join order_items i on o.id = i.order_id
where o.status = 'open'
order by date asc
That nested select statement is what I can't figure out. So, is this possible?
When I run my version of the query I get:
Error Code: 1242
Subquery returns more than 1 row