select is easy to use

grammar

Let’s take a brief look at it first, and we will talk about it later. distinct is used to remove duplicates, from specifies the table name, where statement is used to control the query conditions, order by is used to sort the results in ascending/descending order, and limit is used for paging.

SELECT
[DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...

Note: mysql is not case-sensitive. I am typing on the command line. Most of them are simply written in one line. In order to facilitate the observation of words, lowercase is used. Just use the bag according to the requirements.

Basic query

1. Full column query

Syntax:select * from table name

2. Specify column query

Syntax:select field 1, field 2, field 3... from table name  ( Each field is separated by a comma. Note that the comma here is the English one, not the Chinese one)

3. Rename query results

Syntax:select field 1 new name, field 2 new name, field 3 new name... from table name   (old field separated from the new name by a space)

4. Query to remove duplicates

Syntax:select distinct field 1, field 2.... from table name (distinct is followed by several fields, indicating that this Several fields must be the same to remove duplicates)

If it is select distinct * from table name, it means that all fields in the table must be the same before deduplication will occur. As long as one field is different, duplication will not be deduplicated.

where condition

We can use the where statement to impose some restrictions on the query conditions. For example, if we want to query everyone whose scores are greater than 90 and less than 100, we can use the where statement to filter the conditions.

operator

operator illustrate
>, >=, <, <=
Greater than, greater than or equal to, less than, less than or equal to
=
Equal to, NULL does not participate in the calculation
<=>
Equal to, NULL participates in the calculation
!= not equal to
between a and b The range belongs to [a,b], that is, a<= value <=b
in (a,b,c,d......) The condition is true as long as the result is one of the ranges in the brackets
is NULL Used to determine whether it is NULL
is not NULL Determine whether it is not NULL
like Fuzzy matching, % represents 0 or more arbitrary characters, _ represents an arbitrary character. For example, someone named Sun: 'Sun%', Sun: 'Sun_'

Logical Operators

operator illustrate
and Both sides of and must satisfy the conditions at the same time
or a or b, just satisfy one of ab
not used to obtain the opposite result

Here is an example to explain, first look at this table:

Example 1

Screen students who failed English and their corresponding English scores:

Example 2
Screen students whose Chinese scores are between 80-90 and their corresponding Chinese scores

Use cases with logical operators

Example 1 (including the use of in(...))

Screen students whose math scores are one of 58, 59, 98, or 99 and their math scores

Here we can use or to connect:

You can also use the above operator, in(a,b,c,d...)

Example 2 (use of like fuzzy matching)

Screen out classmate Sun and his Chinese and math scores

Filter out students named Sun and their corresponding total scores

Example 3 (Field comparison can also be performed after where)

Screen out students whose Chinese scores are greater than their math scores, as well as their corresponding Chinese and math scores.

Example 4 (and and not)

Screen out students whose Chinese scores are greater than 80 and whose surname is not Sun and their scores

Example 5 (brackets indicate a large condition)

If it is Sun, screen directly. Others will meet the following requirements: the total score is greater than 200, and Chinese is less than mathematics, and English is greater than 80.

order by to sort the results

The results of the previous query are all unordered. We can sort the results through the order statement. It should be noted here that NULL value is the minimum value in it.

Ascending order

order by xxx asc: Sort in ascending order according to xxx

Example 1:

Still the table above, sorted in ascending order according to Chinese scores

Example 2:

Display in ascending order by total score:

descending order

order by xxx desc:According to xxx, sort the results in descending order

The usage is the same as ascending order, except that asc is changed to desc. The sql statement here defaults to asc if not specified.

example
Query the scores of students in each subject, and display them in descending order of mathematics, ascending order of English, and ascending order of Chinese.
Example (used with where statement)

Query the mathematics scores of people with the surname Cao and Sun, and sort them in descending order of mathematics

limit filter paging results

Sometimes we only want to display 10 pieces of information on one page, so we use limit paging. At this time, jump to the beginning of the chapter and you can see the final limit.

Suggestion: When querying an unknown table, it is best to add a LIMIT 1 to avoid querying the entire table because the data in the table is too large. Data causes database to freeze

Note that the starting subscript of limit starts from 0.

There are three common usages, as follows: Let’s first look at a complete table to experiment

limit n

Starting from 0, filter n results.

limit s,n

Starting from s, display n results

limit n offset s

It has the same meaning as limit s,n, except that compared to limit s,n, I am more accustomed to using this.

You can see the same results as above.

example

This table is divided into 3 pages in descending order by ID, and each page displays 3 records.

At this point, a table is divided into three pages, with 3 records on each page.

After the basic single table query is completed, it can be used in various scenarios, using where, order by, distinct, and limit to flexibly use it according to needs - love your bag.

Guess you like

Origin blog.csdn.net/qq_60192898/article/details/134995185