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