Home > Backend Development > PHP Tutorial > How to count the number of records when using GROUP BY COUNT DISTINCT

How to count the number of records when using GROUP BY COUNT DISTINCT

WBOY
Release: 2016-07-29 08:44:57
Original
1248 people have browsed it

For example, in a table like this, I want to count the number of records with different emails and passwords. Copy the code. The code is as follows: 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 ', ”);
Usually our approach is like this


Copy the code

The code is as follows:


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

What is the result of this? Copy the code

The code is as follows:

COUNT(*)
1
2

1 1 Obviously this is not the result I want. What is counted is the number of records with the same email and passwords Sum, the following is enough



Copy the code

The code is as follows:

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

Of course, you can also use mysql_num_rows in php Get the number of records, but this is not efficient. You can refer to this articlemysql_num_rows VS COUNT efficiency problem analysis The above has introduced how to count the number of records COUNT DISTINCT when using GROUP BY, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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