2013-08-22 38 views
0

我試圖讓客戶使用的5個最新訂單定時:獨立GROUP_CONCAT()導致

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爲所有客戶。

我在哪裏做錯了?

+0

爲什麼你'GROUP BY customer_id'當有頂級查詢無聚集函數? – Barmar

+0

我的意思是o.customer_id,我的不好:) – user2704098

+0

這是一回事。你爲什麼在不彙總時進行分組?只有子查詢聚合,但它不是分組。 – Barmar

回答

0

我不確定「sysdate for all customers」是什麼意思。但是,join沒有達到您的預期。

只是做了一次聚集:

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 
+0

它工作絕對完美:D非常感謝! – user2704098