MySQL Single table query

Source: Internet
Author: User

Simple query:

SELECT * FROM Tb_name

Select Field1,field2 from Tb_name;

SELECT * from Tb_name where qualification;

Example: Select Name,age from students where age+1>20;

Select name from student where age>20 and gender= ' M ';

Select name from student where age>20 or gender= ' M ';

Select name from student where not age>20 and not gender= ' M ';

Select name from student where not (age>20 or gender= ' M ');

Select name from student where age>=20 and age<=50; or select name from student where age between 25;

Select name from student where like name ' Y% ';

The regular expression can be followed REGEXP or rlike:

Select name from student where name Relike ' ^[mny].*$ '; matches any character after the beginning of M or N or y.

In usage: Names of students aged 18, 20, 25

Select name from student where age in (18,20,25);

Show student table in the CID is null classmate;

Select name from student, where CID is null;

is not the notation for null values:

Select name from student where CID is not null;

Sort the data according to the query:

Select name from student where CID is not null order by name;

Three formats for data storage: Heap files, sequential files, hash

to alias a field;

Select name as Student_name from student where CID was not null order by name;

Limit the number of lines displayed (limits);

Example: select Name as Student_name from student as Xiaohai limit 2

Limit only two lines are displayed.

Skip the first two lines:

Select name as Student_name from student as Xiaohai limit 2,3

Skip the first two lines to show three rows.

Aggregation calculation:

Average age of all students:

Select AVG (age) from student;

Select Max (age) from student;

Select min (age) from student;

Select SUM (age) from student; Sum

Select count (age) from student; Find the number

GROUP BY: Grouping

Select Age,gender from student group by gender;

Grouping student tables by gender (i.e. male, female) shows age gender

Select AVG (age) from student group by gender;

Divide the table student by sex and calculate their average.

The primary purpose of grouping is to aggregate:

Note: Filtering on the group by condition must be done with having the function equivalent to where

Select COUNT (cid1) as persons,cid from student group by CID1 have persons>=2;

Having the function is to filter the results of group by again.


This article is from the "Wangconggao" blog, make sure to keep this source http://wangconggao.blog.51cto.com/3253868/1579692

MySQL Single table query

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.