简体   繁体   中英

Adjust sql query to get count with group_concat

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.

You get there in two steps: First count the items, then get all items with their count per order:

select 
  orders.*, 
  DATE_FORMAT(orders.created_at, '%d.%m.%Y %h:%i%p') as date, 
  oi.items 
from orders 
inner join 
(
  select order_id,  group_concat(concat(cnt, ' x ', item_name)) as items 
  from
  (
    select order_id, item_name, count(*) as cnt
    from orders_items 
    group by order_id, item_name
  ) as counted
  group by order_id
) as oi on oi.order_id = orders.id;

Further to the aforementioned query:

select    orders.*,    DATE_FORMAT(orders.created_at, '%d.%m.%Y
%h:%i%p') as date,    oi.items  from orders  inner join  (   select
order_id,  group_concat(concat(cnt, ' x ', item_name)) as items   
from   (
select order_id, item_name, count(*) as cnt
from orders_items 
group by order_id, item_name   ) as counted   group by order_id ) as oi on oi.order_id = orders.id;

I would add a

Having Count(*)>0

to eliminate any items with a 0 quantity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM