0

以下を使用して、顧客の最新の注文タイミングを 5 つ取得しようとしています。

SET SESSION group_concat_max_len = 99;
select o.customer_id, substring_index(m.orders,',', 1) as order1,
    (case when numc >=2 then substring_index(substring_index(m.orders, ',', 2), ',', -1)end) as order2,
    (case when numc >=3 then substring_index(substring_index(m.orders, ',', 3), ',', -1)end) as order3,
    (case when numc >=4 then substring_index(substring_index(m.orders, ',', 4), ',', -1)end) as order4,
    (case when numc >=5 then substring_index(substring_index(m.orders, ',', 5), ',', -1)end) as order5
    from orders o,
         (select group_concat(date order by date desc) as orders, count(*) as numc
          FROM orders) m
where country_id='27' 
group by customer_id

しかし、すべての顧客に対してsysdateが返されます。

どこが間違っていますか?

4

1 に答える 1

0

「すべての顧客の sysdate」の意味がわかりません。しかし、joinあなたが期待することをしていません。

集計を 1 回行うだけです。

select o.customer_id, substring_index(group_concat(date order by date desc),',', 1) as order1,
    (case when count(*) >=2 then substring_index(substring_index(group_concat(date order by date desc), ',', 2), ',', -1)end) as order2,
    (case when count(*) >=3 then substring_index(substring_index(group_concat(date order by date desc), ',', -1)end) as order3,
    (case when count(*) >=4 then substring_index(substring_index(group_concat(date order by date desc), ',', 4), ',', -1)end) as order4,
    (case when count(*) >=5 then substring_index(substring_index(group_concat(date order by date desc), ',', 5), ',', -1)end) as order5
    from orders o
where country_id='27' 
group by customer_id
于 2013-08-22T12:33:36.497 に答える