如果我的查找表包含如下字段:
id,color_id,size_id,price
我可以使用以下方法轻松获得特定颜色的所有尺寸/价格:
SELECT size_id, price from TABLE where color_id = 2
但是,如果我想显示这样的价格表:
red blue green yellow
xs 1.20 1.20 1.29 1.39
sm 1.40 1.40 1.49 1.59
md 1.40 1.40 1.19 1.59
lg 1.55 1.55 1.80 1.94
xl 1.55 1.55 1.80 1.99
xx 2.01 2.05 2.40 2.68
有没有办法这样做而不对每种颜色进行查询?或者有更好的方法来构建数据吗?
感谢您的帮助!
答案 0 :(得分:0)
select size_id, sum(if(color_id=1,price,0)) c1, sum(if(color_id=2,price,0)) c2, sum(if(color_id=3,price,0)) c3, sum(if(color_id=4,price,0)) c4 from TABLE group by size_id
如果您没有可变数量的颜色,至少这很有效。