MySQL Statistics Query implementation code _ MySQL

Source: Internet
Author: User
Tags character classes
MySQL Statistics Query implements code statistics on the number of rows
The select count () FROM syntax is used to COUNT the number of rows in a data table.

Syntax:

Select count (column) FROM tb_name
This SQL syntax is used to COUNT the number of rows in a certain field. COUNT () cannot contain multiple fields, but it can be.

Example:


$ Conn = @ mysql_connect ("localhost", "root", "root123 ");
If (! $ Conn ){
Die ("failed to connect to database:". mysql_error ());
}

Mysql_select_db ("test", $ conn );
$ SQL = "SELECT COUNT (uid) FROM user ";
$ Row = mysql_fetch_array (mysql_query ($ SQL ));
Echo "common users:", $ row [0], "bit ";
?>


Browser display:

Total users: 4

Description
Although the parameter in count () can be a field name, if you only want to count the number of data records in the table, it is recommended to count the primary key count (id) or directly use count (*) for efficiency (*), in addition, avoid using column data attributes as character classes.

Now that we can see this, let's share two examples.

Count in the Mysql Query statistics function

Today, I encountered a question: count the total number of girls whose scores are greater than 90.
At the beginning, I wrote: $ SQL = "select girl's score from use where score> 90"; $ result = mysql_query ($ SQL );
$ Row = mysql_num_rows ($ result); echo "total: $ row ";
But it would be okay for 100. if it was 10000, would it be very slow !! Later, a friend told me to use the count function.
Change the preceding SQL statement:
$ SQL = "select count (*), female score from use group by female score having female score> 90 ";
In this way, the query statement is much faster.

MySQL Query statistics per select statement

Where can I use a single select statement to implement MySQL Query statistics? It is too useful. for example, if you want to query the number of people who pass or fail, how can you query it?
The simple statement for MySQL Query statistics must be as follows:


Select a. name, count_neg, count_plus from
(Select count (id) as count_plus, name from score2 where score> = 60 group by name),
(Select count (id) as count_neg, name from score2 where score <= 60 group by name) B
Where a. name = B. name

That is, at least two statements must be used.

Today, we just found that mysql supports if, so we can use if to implement it creatively:


Select name, sum (if (score> = 60, 1, 0), sum (if (score <60, 1, 0) from score2 group by name

It is easy to use a single select statement to implement MySQL Query statistics.

If the principle is greater than 60, the value is 1, and sum is the count.

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.