我有一张这样的表:
ITEM PICTURE CATEGORY STOCK
1 Mickey Shirt 3
2 Mickey Jacket 2
3 Donald Jacket 0
4 Donald Shirt 6
5 Donald Hoodie 1
6 Mickey Hoodie 3
7 Goofy Shirt 1
8 Goofy Hoodie 2
9 Goofy Jacket 3
ITEM列是自动增量
我需要这样的输出:
PICTURE HOODIE JACKET SHIRT
Donald 1 0 6
Goofy 2 3 1
Mickey 3 2 3
知道如何产生这样的输出吗?谢谢
答案 0 :(得分:2)
您可以使用CASE
表达式。
<强>查询强>
select PICTURE,
sum(case CATEGORY when 'Hoodie' then STOCK else 0 end) as `HOODIE`,
sum(case CATEGORY when 'Jacket' then STOCK else 0 end) as `JACKET`,
sum(case CATEGORY when 'Shirt' then STOCK else 0 end) as `SHIRT`
from your_table_name
group by PICTURE;