2
几张桌子,我有表:复杂的查询 - 用分组的列
Worker (ID, Name)
Box (ID, Name, ID_Worker)
BoxColor (ID, Name)
BoxSize (ID, Name)
Item(ID, ID_box, ID_BoxColor, ID_BoxSize)
所以我们谁创建项目,并把它们自己的箱子里面的工人。 对于我的秘密工作的缘故,我改变了话题,从我的秘密之一EHM箱;)
我想创建raport有列:
Worker.Name | BoxSize.Name | BoxColor(id=0) | BoxColor(id=1) | BoxColor(id=2)
表盒中其它raports使用,因此这桌子的结构可以改变。
我例如想要得到的是:
分组名工人和每个BoxSize,有在列每种颜色计数。 例如:
John | X | 2 | 4 | 0 |
John | XL | 5 | 1 | 0 |
John | XXL | 2 | 0 | 0 |
John | S | 3 | 1 | 0 |
Adam | X | 5 | 4 | 0 |
Adam | XL | 1 | 3 | 0 |
Adam | S | 0 | 1 | 0 |
....
BoxColor是没有那么多颜色的表,以便与不同的子可以硬编码选择像
(select count(*) from BoxColor where ID = 0)
(select count(*) from BoxColor where ID = 1)
(select count(*) from BoxColor where ID = 2)
我试图做这样的事情,但查询答循环它永远不会停止 “思考”
select Worker.Name, BoxSize.Name,
(select count(*) from BoxColor as BoxColor2, Box as Box2, Item as Item2
where Box2.ID_Worker = Worker.ID and
Item2.ID_Box = Box2.ID and
Item2.ID_BoxColor = BoxColor2.ID) and
BoxColor.ID = 0) as Red,
(select count(*) from BoxColor as BoxColor2, Box as Box2, Item as Item2
where Box2.ID_Worker = Worker.ID and
Item2.ID_Box = Box2.ID and
Item2.ID_BoxColor = BoxColor2.ID) and
BoxColor.ID = 1) as Green,
(select count(*) from BoxColor as BoxColor2, Box as Box2, Item as Item2
where Box2.ID_Worker = Worker.ID and
Item2.ID_Box = Box2.ID and
Item2.ID_BoxColor = BoxColor2.ID) and
BoxColor.ID = 2) as Blue,
from Worker, BoxSize, Box, Item
where
Item.ID_Worker = Worker.ID and
Item.ID_Box = Box.ID
Item.ID_BoxSize = BoxSize.ID
group by Worker.Name, BoxSize.Name
having Red > 0, Blue > 0, Green > 0
order by 1, 2
考虑处理应用程序级代码/表示层中的数据显示问题(如果有)(例如,一个简单的PHP循环) – Strawberry
我不能在我的网络应用程序中做这个事情,我提出了添加新报告的灵活方法。还有很多其他的,所以我无法为一个报告创建特定的界面:) – gemGreg