我需要转换我的查询结果,因此具有相同外键的多行只是一行。 我可以通过分组来做到这一点。问题是,在我的一个列中,我想连接一些具有不同分组的列。 例如我的表包含这样的somting:
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将它们分组,并通过使用总和和案例陈述找到每个品牌和类别的总数量是一件容易的事。我的问题是如何连接商店,类别,品牌分组的颜色和数量(总分组是由shopId而非商店,类别,品牌)?
我的标签是
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
我正在寻找一种方法来在每个总和之后添加一个列来指定每个颜色数量,例如,如果HP是15它有7个红色和8个蓝色笔记本。我尝试了GROUP_Concat但由于分组错误而没有显示正确的结果。
答案 0 :(得分:0)
我找到了答案并希望与他人分享。 我在每次求和后的查询中添加了以下代码
,group_concat(case when brand.name='{$brand['name']}' and category={$category['id']} then concat(color.name,':',num) end) as {$brand['name']}C
{$ brand ['name']}和{$ category ['id']}是我的PHP代码中的变量,用于在品牌和类别之间进行迭代 我不知道为什么花了这么长时间才找到这么简单的解决方案:P羞耻我:D