Home > Backend Development > PHP Tutorial > How to count the number of records when using GROUP BY COUNT(*) DISTINCT_PHP tutorial

How to count the number of records when using GROUP BY COUNT(*) DISTINCT_PHP tutorial

WBOY
Release: 2016-07-21 15:30:19
Original
1277 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 code The code is as follows:

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', ”);

Usually this is how we do it
Copy 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 code The code is as follows:

COUNT(*)
1
2
1
1

Obviously this is not the result I want. What is counted is the sum of the number of records with the same email and passwords. The following is enough
Copy code The code is as follows:

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

Of course in php You can also use mysql_num_rows to get the number of records, but this is not efficient. You can refer to this article
mysql_num_rows VS COUNT efficiency problem analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323248.htmlTechArticleFor example, in a table like this, I want to count the number of records with different emails and passwords. Copy the code as follows: CREATE TABLE IF NOT EXISTS `test_users` ( ​​`email_id` int(11) unsigned...
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