How to count the number of record bars count (*) distinct_php tips when using GROUP by

Source: Internet
Author: User
For example such a table, I want to count the number of records with different emails and passwords
Copy 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 () 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 ', ");

Normally, that's what we're doing.
Copy Code code as follows:

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

What is the result?
Copy 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 sum of the number of records, the following is OK
Copy 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 records of the number of bars, but this efficiency is not high, you can refer to this article
Mysql_num_rows VS COUNT Efficiency problem 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.