MySQL Tutorial: SQL Query

The SQL Query Language (a)

Foreword

SQL query is to develop the most used and most important statement that we are now living network and both are going on query operations, such as: open the micro-channel circle of friends to see, Jingdong Taobao stroll merchandise, find some on the Baidu things, brush headlines and so on the phone. Query more flexible, there are many uses, they grasp the development has an important role in our program.


The basic query

The basic syntax of the query is:

select list of fields from table name;

Wherein the field list may contain multiple fields, directly separated by commas, such as:

select field 1, field 2, field 3 from table;

You can also use * to represent all fields, such as:

select * from 表名;

Code Example:

- Query all fields

select * from tb_student;



- the query part of the field

select stu_id,stu_name,stu_age from tb_student;


Query uses the alias display

In the query results, the column name can be replaced with an alias

grammar:

1 alias select field, the field 2 ... from the alias table;

Code Example:

- use aliases when querying

select stu_id number, stu_name name, stu_age Age from tb_student;


WHERE keyword of the query

In most cases, during our inquiry, according to certain conditions you need to filter the results.

Conditional query:

select * from table where conditions;

Code Example:

- Query number of students 2

select * from tb_student where stu_id = 2;

- Query students older than 20

select * from tb_student where stu_age >= 20;


IN the keyword query

It represents any one of a plurality of query field values

Field name in (value 1, value 2 ....)

Code Example:

- Query a native of Wuhan students or Beijing or Chengdu

select * from tb_student where stu_address in ( 'Beijing', 'Wuhan', 'Chengdu');


BETWEEN the keyword query

It represents a value in the range of the field

Field name value between 1 and the value 2

Code Example:

- Query students aged between 20-25

select * from tb_student where stu_age between 20 and 25;

LIKE keyword query of fuzzy query

Sometimes we need to check less precise conditions, such as: surname of the student, the student's name with a small, etc.

Field name like 'with wildcard characters';

Wildcards include:

% Represents the character of any length

_ Stands for any one character

Code Example:

- Query student surnamed Zhang

select * from tb_student where stu_name like '张%';

- Polling for the 3333 end of the phone number of students

select * from tb_student where stu_telephone like '%3333';

- the student's name in the query with small

select * from tb_student where stu_name like '%小%';


IS NULL conditions of the query keyword query

Query field is empty records if the query is not empty field, you can not add

Field is [not] null

- query did not fill out the phone number of students

select * from tb_student where stu_telephone is null;



AND keyword query of multi-criteria query

Use and connect two conditions, two conditions must be true at the same time, the overall condition was established

Condition 1 and Condition 2

Code Example:

- Query female students aged 25 or more

select * from tb_student where stu_age > 25 and stu_gender = '女';


OR keyword query of multi-criteria query

Use or to connect two conditions, only one condition was established, the entire conditional on the establishment

Condition 1 or Condition 2

Code Example:

- Query a native of Beijing or Wuhan male students

select * from tb_student where stu_address = '北京' or stu_address = '武汉';


The DISTINCT keyword query

Query results will be many field values ​​are duplicated, distinct can remove duplicate values.

select distinct field from the table ...;

Code Example:

- Query for all students Birthplace

select distinct stu_address from tb_student;





Search Keyword The ORDER BY ordering

We can sort field query, such as commodity sorted by price, volume, etc.

Sort syntax is:

select * from table name order by field [asc | desc];

followed sorted order by field, asc indicate ascending, the default value may be omitted, desc descending order.

Code Example:

- Students sorted in ascending order by age

select * from tb_student order by stu_age;

- Students sorted in descending order by age

select * from tb_student order by stu_age desc;



LIMIT keyword query pagination of

Sometimes we are more tables inside of data, the query can be divided into multiple pages, so that the query speed will increase, the user is relatively easy to operate.

grammar:

select * from table limit start position, length;

Description: start position is zero, the position of the first row is 0, the start position may be omitted, the default is from the first row. The number of lines of the page length is recorded.

Code Example:

- Query student, every five acts of a divided multi-page inquiry

select * from tb_student limit 0,5;

select * from tb_student limit 5,5;

select * from tb_student limit 10,5;

- Query the youngest students

select * from tb_student order by stu_age limit 1;

to sum up

In this chapter we learned the basic query that contains queries where the conditions, in the keyword query multiple values, between keyword search any connection or keywords and use and conditions, as well as order by sorting, limit pagination. Later we will learn in the connection, outer joins, sub-queries and other advanced query method.


Guess you like

Origin blog.csdn.net/weixin_34415923/article/details/91397373
Recommended