是否有任何理由不使用表#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