Considerations for using mysql count distinct null

Source: Internet
Author: User

Note 1 For mysql count distinct null: An example is used to explain the problem. Now another database table is hello. The table content is as follows: www.2cto.com id name 1 Null2 Null3 Null4 Null5 Null hello table has two fields: id and name, name is null. The first SQL statement www.2cto.com: SELECT COUNT (id) FROM hello; query result: 5, correct. The second SQL statement: SELECT COUNT (*) FROM hello; query result: 5, correct. Article 3 SQL: SELECT COUNT (name) FROM hello; query result: 0, error. Article 4 SQL: SELECT COUNT (DISTINCT id, name) FROM hello; query result: 0, error. 2. Causes of errors in the second and third SQL queries: 2.1 COUNT (), MIN (), and SUM () ignore NULL values. 2.2 The exception to this is COUNT (*), which counts rows and not individual column values. 2.3 For example, the following statement produces two counts. the first is a count of the number of rows in the table, and the second is a count of the number of non-NULL values in the age column: mysql> select count (*), COUNT (age) FROM person;

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.