2010-02-10 79 views
1

我有一個從多個表中獲取數據的存儲過程。存儲過程從PHP腳本中調用,並將生成的數據放入網格中。MySQL - 分組dayname()爲唯一列

我遇到的問題是我不知道如何讓dayname獨一無二,以至於最多有七個列,一個星期中的每一天,而不是像'週二'和 'T​​uesday1'

這裏是一個片段

select concat(
       'select tbl_kiosks.name "DCI ERP",tbl_retailers.abbr "Retailer", 
    tbl_retaillocations.storeNumber "Store", 
    tbl_retaillocations.city "City", 
    tbl_retaillocations.state "State"' 
      , group_concat(
        concat(',sum(if(ks.StartDate="',ks3.StartDate,'", numOfSessions, null)) "', dayname(DATE_FORMAT(ks3.StartDate,'%Y-%m-%d')) ,'" ' 
        ) 
        separator ' ' 
       ) 

回答

0

在某處,group_concat,你必須select distinct dayname(...)而不是select dayname(...)。那會讓你每天只有一列。

或者你可以考慮group by

select 
    tbl_kiosks.name "DCI ERP" 
, tbl_retailers.abbr "Retailer" 
, tbl_retaillocations.storeNumber "Store" 
, tbl_retaillocations.city "City" 
, tbl_retaillocations.state "State" 
, dayname(ks3.StartDate) "Weekday" 
, sum(case when dayname(ks3.StartDate) = 'Monday' 
     then numOfSessions else 0 end) as Monday 
, sum(case when dayname(ks3.StartDate) = 'Tuesday' 
     then numOfSessions else 0 end) as Tuesday 
, .... 
from <your join list here> 
group by "DCI ERP", Retailer, Store, City, State, dayname(ks3.StartDate) 
+0

我真的試圖這樣做,但出於某種原因選擇DAYNAME截然不同產生相同的結果。我仍然在嘗試,所以如果我把它整理出來,我會回覆更多的信息。 – AmateurHour 2010-02-10 20:38:52