How to count the number of record bars when using group by Count DISTINCT

Source: Internet
Author: User
For example such a table, I would like to count the number of records for which email and passwords are not the same

Copy the Code code as follows:


CREATE TABLE IF not EXISTS ' test_users ' (
' email_id ' int (one) unsigned not NULL auto_increment,
' Email ' char (+) not NULL,
' Passwords ' char (+) is 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 ', ");
That's what we're usually doing.

Copy the Code code as follows:


SELECT COUNT (*) from test_users WHERE 1 = 1 GROUP by email,passwords


What is the result of this?

Copy the Code code as follows:


COUNT (*)
1
2
1
1


Obviously this is not the result I want, so the statistics are the same email and passwords the number of records of the sum of the following so that it can be

Copy the Code code as follows:


SELECT COUNT (DISTINCT email,passwords) from ' test_users ' WHERE 1 = 1


Of course, in PHP can also use mysql_num_rows to get the number of records, but this is not high efficiency, you can refer to this article
Mysql_num_rows VS COUNT Efficiency problem analysis

The above describes how to use group by when counting the number of records count DISTINCT, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.