0
我需要轉換我的查詢結果,因此多行具有相同的外鍵只是一行。 我可以做到這一點與分組。問題是在我的一個列中,我想用不同的分組連接一些列。 例如我的表包含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 Dell Color HP Color etc...
-----------------------------------------------------------
15 6 red:5, Blue:1 2 red:2
14 ..............
我的查詢
我通過shopId和使用金額和案例statment是一個很容易找到工作的每個品牌和類別的總數量劃分它們。我的問題是我如何連接店鋪,類別,品牌分組的顏色和數量(總分組由shopId不是商店,類別,品牌)?
我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
和我的不完備查詢
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
我正在尋找一種方式,每個總和後添加一列用於指定每種顏色的數量,例如,如果惠普是15它有7個紅色和8個藍色筆記本。我嘗試了GROUP_Concat,但由於分組錯誤,它沒有顯示正確的結果。
你想要一個「樞軸」。搜索那個術語 – Bohemian 2012-08-13 11:42:12
我已經在搜索那個。但坦克:) – Soheil 2012-08-13 12:05:43
向我們展示您的表結構(SHOW CREATE TABLE your_table'的結果)和您的查詢到目前爲止。 – Jocelyn 2012-08-13 13:39:13