今天突然想到了數據庫中的行專列與列轉行,還不熟悉,在上網看了一下然后就自己寫了個例子。
數據庫表示這樣滴!

//全部查詢出來
SELECT (case type when 'MySql數據庫' then id else NULL END) as 'MySql數據庫', (case type when 'SqlServer數據庫' then id else NULL END) as 'SqlServer數據庫', (case type when 'CSharp' then id else NULL END) as 'CSharp', (case type when 'Html' then id else NULL END) as 'Html' from xiaoyao_blogs_essay;
//查詢出他們id的和 SELECT sum(case type when 'MySql數據庫' then id else NULL END) as 'MySql數據庫', sum(case type when 'SqlServer數據庫' then id else NULL END) as 'SqlServer數據庫', sum(case type when 'CSharp' then id else NULL END) as 'CSharp', sum(case type when 'Html' then id else NULL END) as 'Html' from xiaoyao_blogs_essay;
//查詢出最大的id SELECT max(case type when 'MySql數據庫' then id else NULL END) as 'MySql數據庫', max(case type when 'SqlServer數據庫' then id else NULL END) as 'SqlServer數據庫', max(case type when 'CSharp' then id else NULL END) as 'CSharp', max(case type when 'Html' then id else NULL END) as 'Html' from xiaoyao_blogs_essay; //查詢出最小的id SELECT min(case type when 'MySql數據庫' then id else NULL END) as 'MySql數據庫', min(case type when 'SqlServer數據庫' then id else NULL END) as 'SqlServer數據庫', min(case type when 'CSharp' then id else NULL END) as 'CSharp', min(case type when 'Html' then id else NULL END) as 'Html' from xiaoyao_blogs_essay;
//查詢出每列的總數 SELECT count(case type when 'MySql數據庫' then id else NULL END) as 'MySql數據庫', count(case type when 'SqlServer數據庫' then id else NULL END) as 'SqlServer數據庫', count(case type when 'CSharp' then id else NULL END) as 'CSharp', count(case type when 'Html' then id else NULL END) as 'Html' from xiaoyao_blogs_essay;
結果





