end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

mysqlのcase when によるクロス集計

http://torhamzedd.blogspot.jp/2010/06/mysql.html
↑こちらに書かれている通り、↓こう。

select name,  
count(case when color='白' then 1 else null end) as '白',  
count(case when color='赤' then 1 else null end) as '赤',  
count(case when color='青' then 1 else null end) as '青',  
count(case when color='緑' then 1 else null end) as '緑'  
from history; 

よく忘れるので、メモ。