是否有任何理由不使用表#1的視圖?
即
Create View color AS
select r.red,blue,green from
(select count(color) as red from selected_colours where color='red') AS r,
(select count(color) as red from selected_colours where color='blue') AS b,
(select count(color) as red from selected_colours where color='green') AS g
或者你的更新查詢應該是
update color set red=r.red,blue=b.blue,green=g.green from
(select count(color) as red from selected_colours where color='red') AS r,
(select count(color) as red from selected_colours where color='blue') AS b,
(select count(color) as red from selected_colours where color='green') AS g
編輯 - 有關動態顏色註釋之後。
您應該創建一個視圖
Create View color AS
select color, count(color) as count from selected_colors group by color
這會給你一個有一排用其計數每一種顏色,你不能簡單的創建一個查詢,會動態改變的結果的結構(例如,改變列)。它的可能性,但它會與動態查詢創建很多工作,它不會有效。
太棒了!但是有可能把它放到一個循環中來自動迭代每種顏色。目前我有超過60種顏色。對不起,我是SQL中的noob,感謝您的回覆。 – Ronald