Home > Database > Mysql Tutorial > mysql获取group by总记录行数的方法

mysql获取group by总记录行数的方法

WBOY
Release: 2016-06-07 16:27:52
Original
1452 people have browsed it

本文实例讲述了mysql获取group by总记录行数的方法,分享给大家供大家参考。具体方法分析如下: 一般来说,mysql获取group by内部可以获取到某字段的记录分组统计总数,而无法统计出分组的记录数。 mysql中可以使用SQL_CALC_FOUND_ROWS来获取查询的行数,在

本文实例讲述了mysql获取group by总记录行数的方法,分享给大家供大家参考。具体方法分析如下:

一般来说,mysql获取group by内部可以获取到某字段的记录分组统计总数,而无法统计出分组的记录数。

mysql中可以使用SQL_CALC_FOUND_ROWS来获取查询的行数,在很多分页的程序中都这样写:

SELECT COUNT(*) from `table` WHERE ......;


查出符合条件的记录总数:

SELECT * FROM `table` WHERE ...... limit M,N;


查询当页要显示的数据 这样的语句可以改成:

SELECT SQL_CALC_FOUND_ROWS * FROM `table` WHERE ...... limit M, N;
SELECT FOUND_ROWS();


因此可以配合mysql自带的SQL_CALC_FOUND_ROWS和FOUND_ROWS()函数可以实现:

SELECT SQL_CALC_FOUND_ROWS t3.id, a,bunch,of,other,stuff FROM t1, t2, t3 WHERE(associate t1,t2,and t3 with each other) GROUPBY t3.id LIMIT 10,20SELECT FOUND_ROWS() as count;


使用以上两个语句即可完成满足group by的总记录行数。

补充:

group by 的简单使用方法:

'SELECT column_id,count(*) as count FROM my_table group by column_id';

希望本文所述对大家的MySQL数据库程序设计有所帮助。

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