Home >Backend Development >PHP Tutorial >能否一次查询取出各栏目记录的总数,该怎么处理

能否一次查询取出各栏目记录的总数,该怎么处理

WBOY
WBOYOriginal
2016-06-13 10:27:27870browse

能否一次查询取出各栏目记录的总数
MySQL某表中有一字段:class_id

class_id
1
1
2
2
2
5
3
3
……

如何一次(或高效)查询,得出各栏目记录总数。即:
class_id为1的记录有2条、2的有3条、5的有1条、3的为2条,……



------解决方案--------------------

PHP code
Create TableCREATE TABLE `demo` (  `demoId` int(11) NOT NULL AUTO_INCREMENT,  `class_id` int(11) DEFAULT NULL,  PRIMARY KEY (`demoId`)) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8//2 录入测试数据//3 查询SELECT COUNT(class_id),class_id FROM demo GROUP BY class_id<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn