내 쿼리 결과를 변환해야하므로 동일한 외래 키가있는 여러 행이 단 하나의 행이됩니다. 그룹화로 할 수 있습니다. 문제는 내 열 중 하나에서 다른 그룹으로 일부 열을 연결하려는 것입니다. 예를 들어 내 표는이 같은 somting 포함 :MySQL 사용자 정의 그룹 concat
shopId Brand Category Color QTY
----------------------------------------------
15 Dell NoteBook Red 5
15 Dell NoteBook Blue 1
15 HP NoteBook red 2
15 HP NetBook red 3
14 Sony NoteBook yellow 1
14 Acer Tablet red 10
브랜드, 종류와 색상 모든 외부 키에서 검색. 내가 shopId로하고 쉬운 일을 합 경우 한 Statment을했다 사용하여 각 브랜드 및 카테고리의 총 수량을 찾는 그들을 그룹화 내 쿼리
ShopId Dell Color HP Color etc...
-----------------------------------------------------------
15 6 red:5, Blue:1 2 red:2
14 ..............
같은이 결과를 제시하고자합니다. 내 문제는 상점, 카테고리, 브랜드 그룹화 (총 그룹화는 상점, 카테고리, 브랜드가 아닌 상점)에 대한 색상 및 수량을 어떻게 연결할 수 있습니까?
내 tabale는
CREATE TABLE `shopstorage` ( `color` int(11) NOT NULL, `category` int(11) NOT NULL, `brand` int(11) NOT NULL, `shop` int(11) NOT NULL, `date` date NOT NULL, `qty` tinyint(4) NOT NULL, PRIMARY KEY (`color`,`category`,`brand`,`shop`,`date`), KEY `clrEpClr` (`color`), KEY `clrEpCat` (`category`), KEY `clrEpshop` (`shop`), KEY `clrEpBrand` (`brand`), CONSTRAINT `SSBrand` FOREIGN KEY (`brand`) REFERENCES `brand` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `SSCat` FOREIGN KEY (`category`) REFERENCES `productcategory` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `SSClr` FOREIGN KEY (`color`) REFERENCES `color` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `SSShop` FOREIGN KEY (`shop`) REFERENCES `shop` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=utf8
이고 내가 HP가 15 인 경우, 예를 들어 각 색상 수량을 지정하기위한 각각의 합이 다음에 열을 추가 할 수있는 방법을 찾고
select shop.name, shop.floor, shop.no, sum(case when brand.name ='ASUS' and productcategory.name='NoteBook' then shopstorage.qty else 0 end) as ASUS, sum(case when brand.name ='HP' and productcategory.name='NoteBook' then shopstorage.qty else 0 end) as HP, sum(case when brand.name ='Sony' and productcategory.name='NoteBook' then shopstorage.qty else 0 end) as Sony, sum(case when brand.name ='Dell' and productcategory.name='NoteBook' then shopstorage.qty else 0 end) as Dell, sum(case when brand.name ='ASUS' and productcategory.name='NetBook' then shopstorage.qty else 0 end) as ASUS, sum(case when brand.name ='HP' and productcategory.name='NetBook' then shopstorage.qty else 0 end) as HP, sum(case when brand.name ='Sony' and productcategory.name='NetBook' then shopstorage.qty else 0 end) as Sony, sum(case when brand.name ='Dell' and productcategory.name='NetBook' then shopstorage.qty else 0 end) as Dell, sum(case when brand.name ='ASUS' and productcategory.name='Tablet' then shopstorage.qty else 0 end) as ASUS, sum(case when brand.name ='HP' and productcategory.name='Tablet' then shopstorage.qty else 0 end) as HP, sum(case when brand.name ='Sony' and productcategory.name='Tablet' then shopstorage.qty else 0 end) as Sony, sum(case when brand.name ='Dell' and productcategory.name='Tablet' then shopstorage.qty else 0 end) as Dell from shopstorage join brand on shopstorage.brand=brand.id join color on shopstorage.color=color.id join productcategory on shopstorage.category=productcategory.id join shop on shop.id = shopstorage.shop group by shopstorage.shop
내 incomplet 쿼리입니다 그것에는 7 개의 빨간과 8 개의 블루 노트가 있습니다. 나는 GROUP_Concat을 시도했지만 잘못된 그룹화 때문에 올바른 결과를 보여주지 못했습니다.
당신은 "피벗"을 원한다. 해당 용어를 찾으려면 – Bohemian
이미 검색했습니다. 하지만 탱크 :) – Soheil
우리에게 당신의 테이블 구조 ('SHOW CREATE TABLE your_table'의 결과)와 지금까지의 쿼리를 보여주세요. – Jocelyn