我在數據庫中有兩列名稱和年份的數據。像這樣的,MySql查詢從數據庫中以矩陣形式提取數據
name | year
-----------
tim | 2001
ron | 2002
tim | 2003
ron | 2005
tim | 2004
pol | 2002
pol | 2001
tim | 2003
pol | 2004
tim | 2002
我想輸出結果矩陣是這樣的。
tim | pol | ron
2001 1 | 1 | 0
2002 1 | 1 | 1
2003 2 | 0 | 0
2004 1 | 1 | 0
2005 0 | 0 | 1
tim | pol | ron根據累計和即tim(5),pol(3),ron(2)以降序排列。
數據不限於tim,pol,ron。可以有n個不同的名字。
請參閱http://stackoverflow.com/questions/7674786/mysql-pivot-table –