I have the following query which lists orders along with items ordered.
select
`orders`.*,
DATE_FORMAT(orders.created_at, '%d.%m.%Y %h:%i%p') as date,
`oi`.`items`
from `orders`
inner join
(
select
order_id,
count(item_name) as count,
group_concat(item_name SEPARATOR ",") as items
from orders_items
group by order_id
)
as oi on `oi`.`order_id` = `orders`.`id`
My tables are setup like so:
orders
id
1
2
3
orders_items
id order_id item_name
1 1 Class Voucher
2 1 Class Voucher
3 1 Class Voucher
4 1 The Cook Book
In the current query, the items column looks something like this when I output it:
Class Voucher,Class Voucher,Class Voucher,The Cook Book
How can I alter it so that each item has a count next to it, instead of listing out each item name. So I want items to contain this instead:
3 x Class Voucher
1 x The Cook Book
Thanks in advance.
orders.created_at( this is not currently part of your posted structure )COUNT(*)unless you need to exclude null values from the count (this mostly comes up when counting rows in aLEFT JOIN).