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

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

WBOY
Release: 2016-06-13 13:27:52
Original
941 people have browsed it

能否一次查询取出各栏目记录的总数
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 Table

CREATE 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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template