How to COUNT the number of records when using group by count (*) DISTINCT

Source: Internet
Author: User

For example, in a table, I want to count the number of records with different email and passwords. Copy codeThe 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,
'Password' char (64) not null,
Primary key ('email _ id ')
) ENGINE = MyISAM default charset = utf8 AUTO_INCREMENT = 6;

Insert into 'test _ users' ('email _ id', 'email ', 'Password') 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 ',");

We usually do this.Copy codeThe Code is as follows: select count (*) FROM test_users WHERE 1 = 1 group by email, passwords

What is the result?Copy codeThe Code is as follows: COUNT (*)
1
2
1
1

Obviously, this is not the result I want. In this way, the sum of the number of records in the same email and passwords is calculated. The following figure shows the result.Copy codeThe 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 to obtain the number of records, but this is not efficient. You can refer to this article.
Mysql_num_rows vs count Efficiency Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.