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

我遇到的问题是我不确定如何使日期名称唯一,以便最多有七列,一周的每一天对应一列,而不是像“星期二”和“星期二1”这样的结果

这是一个片段

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

最佳答案

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)

关于sql - MySQL-为唯一列分组dayname(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2239719/

10-10 06:51