The Count function instance of SQL

Source: Internet
Author: User

Iamlaosong

do software development often and database dealings, will use the SQL language, no one can not avoid using count this function to count the number of record rows, count (1) and COUNT (*) function is the same, all count the number of rows . The 1 here does not mean the first field, but rather a constant, you can actually think of a field in a table, the value of this field is fixed to 1,count (1), that is, how many 1 is calculated. Similarly, COUNT (2), can also, get the value exactly the same, count (' x '), count (' Y ') is all yes. * denotes all fields, equivalent to all fields connected together, including rowID,count (*) counts all records.

Select COUNT (*) from Song_temp t;select count (rowid| | t.mail_num| | T.descrip) from Song_temp t;

you can change the * to a field name, which is Count (field name) or multiple field names can be joined together, but note that this notation only counts the number of records for this field or non-null values for these fields, and the null value is not counted (other functions such as AVG, SUM also exclude null values).

with C ount (field name) Also note that if the field values are duplicated, the number of records will be counted repeatedly, if you need to count the number of different values, you need to use Count (distinct field name), for example, suppose there is a person table PP, There is a field that is "gender", as follows (no gender is entered in the last line):

Name Sex

Zhang Sanfeng Male

Zhang Mowgli Man

Zhou Zhijo Women

Zhao Women

The East is undefeated

Select count (Gender) from PP;

for the above table, the result of count (1) and COUNT (*) is the same, which is 5;soCOunt (gender) willstatistics on the number of people who entered the sex, should be 4,Count (Distinct sex) will count several genders, and the null value without sex is not as one, the result should be 2.



Instance of Count function for SQL

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.