2016-01-15 3 views
2

У меня есть небольшая проблема с моим запросом в mySQL. Мне нужно форматировать все приложения по горизонтали, а не по вертикали.Формат результата MYSQL QUERY

Это мой 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

+0

Где вы хотите использовать эти данные? Вам нужно предоставить дополнительную информацию. И я не думаю, что вы можете форматировать это с помощью SQL. Вам нужно извлечь данные из БД, а затем форматировать/использовать их так, как вы хотите, с помощью технологии, для которой вы ее используете. – Milkncookiez

+0

Мне нужно сопоставить все данные в одной таблице. Мне нужно показывать по месяцам приложения клиентом в одну строку. – EzzeOnursito

ответ

0

Попробуйте;

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 

Если вам нужны данные STORE A затем добавить где условие

where client = 'STORE A' 
+0

это решение !!!! благодаря!!!!!!! ilovu – EzzeOnursito

0

Это работает для вас?

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

почти. Мне нужно показывать месяцы отдельно с его приложениями – EzzeOnursito

Смежные вопросы