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