search
Homephp教程php手册使用GROUP BY的时候如何统计记录条数 COUNT(*) DISTINCT

例如这样一个表,我想统计email和passwords都不相同的记录的条数

复制代码 代码如下:


CREATE TABLE IF NOT EXISTS `test_users` (
`email_id` int(11) unsigned NOT NULL auto_increment,
`email` char(100) NOT NULL,
`passwords` char(64) NOT NULL,
PRIMARY KEY (`email_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;


INSERT INTO `test_users` (`email_id`, `email`, `passwords`) VALUES
(1, ‘jims@gmail.com', ‘1e48c4420b7073bc11916c6c1de226bb'),
(2, ‘jims@yahoo.com.cn', ‘5294cef9f1bf1858ce9d7fdb62240546′),
(3, ‘default@gmail.com', ‘5294cef9f1bf1858ce9d7fdb62240546′),
(4, ‘jims@gmail.com', ”),
(5, ‘jims@gmail.com', ”);

通常我们的做法是这样

复制代码 代码如下:


SELECT COUNT(*) FROM test_users WHERE 1 = 1 GROUP BY email,passwords


这样的结果是什么呢?

复制代码 代码如下:


COUNT(*)
1
2
1
1


显然这不是我要的结果,这样统计出来的是相同email和passwords的各个记录数量之和,下面这样就可以了

复制代码 代码如下:


SELECT COUNT(DISTINCT email,passwords) FROM `test_users` WHERE 1 = 1


当然在php里面也可以用mysql_num_rows来获取记录的条数,但是这样的效率不高,可以参考这篇文章
mysql_num_rows VS COUNT 效率问题分析
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use