Mysql column Change statistics query, row to column statistics query

Source: Internet
Author: User

-- ------------------------------ Table structure for `TabName`-- ----------------------------DROPTABLEIF EXISTS `TabName`;CREATETABLE`TabName` (  `Id` int(11) NOTNULL AUTO_INCREMENT,  `Name` varchar(20) DEFAULTNULL,  `Date` dateDEFAULTNULL,  `Scount` int(11) DEFAULTNULL,  PRIMARYKEY(`Id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULTCHARSET=utf8;-- ------------------------------ Records of TabName-- ----------------------------INSERTINTO`TabName` VALUES(‘1‘, ‘小说‘, ‘2013-09-01‘, ‘10000‘);INSERTINTO`TabName` VALUES(‘2‘, ‘‘, ‘2013-09-01‘, ‘20000‘);INSERTINTO`TabName` VALUES(‘3‘, ‘小说‘, ‘2013-09-02‘, ‘30000‘);INSERTINTO`TabName` VALUES(‘4‘, ‘‘, ‘2013-09-02‘, ‘35000‘);INSERTINTO`TabName` VALUES(‘5‘, ‘小说‘, ‘2013-09-03‘, ‘31000‘);INSERTINTO`TabName` VALUES (‘6‘, ‘‘, ‘2013-09-03‘, ‘36000‘);INSERTINTO`TabName` VALUES(‘7‘, ‘小说‘, ‘2013-09-04‘, ‘35000‘);INSERTINTO`TabName` VALUES(‘8‘, ‘‘, ‘2013-09-04‘, ‘38000‘);-- -------------------------- 查看数据-- ------------------------SELECT* fromTabName ;
-- -------------------------- 列转行统计数据-- ------------------------SELECTDate,MAX(CASENAMEWHEN‘小说‘THENScount ELSE0 END) 小说,MAX(CASENAMEWHEN‘‘THEN Scount ELSE0 END) FROMTabName  GROUPBYDate<br><br>

  

------ -------------------- --row-to-column statistics -------------- ------------
<br select &NBSP;&NBSP;&NBSP;&NBSP; Code class= "SQL keyword" >date name ,scount) as  b_str From    tabname &NBSP; group  by  date
selectDate,NAME, group_concat(NAME,‘总量:‘,Scount) as b_str from   TabName  group by Date,NAME

Mysql column Change statistics query, row to column statistics query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.