MySQL - 为唯一列分组dayname()

时间:2010-02-10 19:31:00

标签: sql mysql

我有一个从多个表中获取数据的存储过程。从PHP脚本调用存储过程,结果数据放在网格中。

我遇到的问题是我不确定如何让dayname独一无二,以便最多有七列,一周中的每一天,而不是得到像'Tuesday'这样的结果'Tuesday1'

这是一个片段

    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 '  '
                 )

1 个答案:

答案 0 :(得分: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)