格式化MYSQL QUERY结果

时间:2016-01-15 14:15:24

标签: mysql

我在mySQL中的查询有点问题。我需要水平地格式化所有应用程序,而不是垂直格式化。

这是我的SQL表:

create table `data` (
    `client` varchar (150),
    `monthh` varchar (150),
    `apps` int (50)
); 
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','january','100');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','february','90');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','february','50');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','march','0');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','may','0');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','june','185');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','july','220');
insert into `data` (`client`, `monthh`, `apps`) values('STORE A','august','0');
insert into `data` (`client`, `monthh`, `apps`) values('STORE B','march','0');
insert into `data` (`client`, `monthh`, `apps`) values('STORE B','april','185');
insert into `data` (`client`, `monthh`, `apps`) values('STORE B','may','165');
insert into `data` (`client`, `monthh`, `apps`) values('STORE B','june','0');
insert into `data` (`client`, `monthh`, `apps`) values('STORE B','august','140');
insert into `data` (`client`, `monthh`, `apps`) values('STORE C','august','100');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','january','117');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','february','103');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','march','129');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','april','112');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','may','115');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','june','111');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','july','111');
insert into `data` (`client`, `monthh`, `apps`) values('STORE D','august','109');

这是我的疑问:

SELECT client,monthh,apps FROM data WHERE client = 'STORE A'

现在...例如我有:

client     monthh     apps
STORE A    january    100
STORE A    february   90
STORE A    february   50
STORE A    march      0
STORE A    may        0
STORE A    june       185
STORE A    july       220
STORE A    august     0

但我需要

client january february march april may june jule august september october november december
STORE A 100 90 50 0 0 185 220 0
你帮我吗?

对不起我的错误解释。我的英语很糟糕。

:d

2 个答案:

答案 0 :(得分:0)

这对你有用吗?

 SELECT client,group_concat(monthh),group_concat(apps) FROM data WHERE client = 'STORE A' group by client

答案 1 :(得分:0)

尝试;

email

如果您需要select client, max(case when monthh = 'january' then apps end) january, max(case when monthh = 'february' then apps end) february, max(case when monthh = 'march' then apps end) march, max(case when monthh = 'april' then apps end) april, max(case when monthh = 'may' then apps end) may, max(case when monthh = 'june' then apps end) june, max(case when monthh = 'july' then apps end) jule, max(case when monthh = 'august' then apps end) august, max(case when monthh = 'september' then apps end) september, max(case when monthh = 'october' then apps end) october, max(case when monthh = 'november' then apps end) november, max(case when monthh = 'december' then apps end) december from data group by client 的数据,请添加where条件

STORE A