当前位置:网站首页>MySQL: single table query

MySQL: single table query

2022-06-09 07:29:00 Cat fearless mouse a

Single table query

1、 One of the most important functions of database management system is data query , Data query should not simply query the data stored in the database , The data should also be filtered as needed , And determine what format the data is displayed

2、MySQL The basic statement for querying data from a data table is SELECT sentence

3、 Single table query refers to querying the required data from a table

4、 Query methods mainly include : Query all fields 、 Query the specified field 、 Query the specified record 、 Query null value 、 Multi condition query 、 Sort the query results

notes : The data of this query is based on the following figure , Some of the values may be changed temporarily

 

Query all fields

1、 grammar : stay SELECT Use asterisks in statements (*) Wildcard Queries all fields
    ⑴SELECT * FROM Table name ;

2、 effect : When using the asterisk wildcard , All columns will be returned , The columns are displayed in the order in which the table was defined

example 1:

mysql> SELECT * FROM polls_article;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms    |  Monkey king made a scene in heaven   |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1  |  At the end of the Eastern Han Dynasty, there were three kingdoms   |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions      |  Grandma Liu enters the Grand View Garden   |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin      |  to join rebels        |   140 |  Shi Naian   | 2020-09-13 12:48:30.962070 |
|  7 | wed       | dwd            |   123 | 32     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
+----+-----------+----------------+-------+--------+----------------------------+
6 rows in set (0.00 sec)

 

Query the specified field

1、 There are many columns in a database table , When querying data , There may be some columns of data that we don't need . Therefore, you can restrict the columns of the query in the query statement

Query a single field

1、 grammar :SELECT Name FROM Table name ;

2、 effect : Query a field in the table ( Column )

example 2:

mysql> SELECT title FROM polls_article;
+-----------+
| title     |
+-----------+
|  The romance of The Three Kingdoms   |
|  The romance of The Three Kingdoms 1 |
|  A dream of red mansions     |
|  Water margin     |
| wed       |
| ds        |
+-----------+
6 rows in set (0.00 sec)


Query multiple fields

1、 Use SELECT sentence , Data under multiple fields can be obtained
    ⑴ You just need to SELECT Then specify the name of the field to look for . Use English commas between different field names (,) Separate , The last field name needs to be followed by a comma

2、 grammar :SELECT Field name 1, Field name 2,..., Field name n FROM Table name

example 2_1:

mysql> SELECT title,price,author FROM polls_article;
+-----------+-------+--------+
| title     | price | author |
+-----------+-------+--------+
|  The romance of The Three Kingdoms   |   132 |  Wu chengen  |
|  The romance of The Three Kingdoms 1 |   154 |  Luo Guanzhong  |
|  A dream of red mansions     |   154 |  Cao xueqin  |
|  Water margin     |   140 |  Shi Naian  |
| wed       |   123 | 32     |
| ds        |   123 | 32     |
+-----------+-------+--------+
6 rows in set (0.00 sec)

 

Query specified data

1、 The database contains a lot of data . But in practice, we may not need all the data , It's part of it ( Data that satisfies certain conditions )

2、 therefore , More time , When we query the data , You need to filter the data . Only the part of data we need is returned

3、 stay SELECT In the sentence , adopt WHERE Clause to filter the data

4、 The grammar is :SELECT Field name 1, Field name 2,..., Field name n FROM Table name WHERE Query criteria


where Clause operator

1、 When filtering data according to conditions , Generally, there are some filter operators as follows

= be equal to
<>     It's not equal to
!=     It's not equal to
<     Less than
<=     Less than or equal to
>     Greater than
>=     Greater than or equal to
BETWEEN       Between the two values specified
is null The value is null
is not null Values are not for null

example 3:

--  The inquiry price is greater than 140 Of books 
mysql> SELECT * FROM polls_article WHERE price > 140;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
2 rows in set (0.00 sec)

example 3_1:

--  The author of the query is ' Cao xueqin ' Some information about books 
mysql> SELECT id,title,author,create_time FROM polls_article WHERE author = ' Cao xueqin ';
+----+--------+--------+----------------------------+
| id | title  | author | create_time                |
+----+--------+--------+----------------------------+
|  3 |  A dream of red mansions  |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
+----+--------+--------+----------------------------+
1 row in set (0.00 sec)

example 3_2:

-- Query data created before a certain time 
mysql> SELECT * FROM polls_article WHERE create_time <= '2020-09-13 12:48:30.962070';
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
3 rows in set (0.00 sec)

notes :
1、 Here are some filter conditions , The other special ones will be introduced separately later

2、 these SQL The statement means : from polls_article Query the specified column in the table , But not all rows are returned , Only the data rows matching the conditions are returned

 

belt IN Keyword query

1、IN The operator is used to query data that meets the conditions within a specified range (IN Operation completion and OR Same function )

2、 Use IN The operator , Enclose all search conditions in parentheses , Search conditions are separated by commas . As long as a value within the condition range is met, it is a match

3、 Contrary , have access to NOT To retrieve data that is no longer within the condition range ( To express with IN The opposite meaning , That is to say, do not select )

4、IN List items not only support numbers , It also supports characters and even time and date types , And these different types of data items can be mixed and arranged

5、 One IN Range comparison can only be performed on one field , If you want to specify more fields , have access to AND or OR Logical operators
 
example 4:

--  The inquiry price is 130 or 154 or 140 Book information of 
mysql> SELECT * FROM polls_article WHERE price IN (130,154,140);
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
3 rows in set (0.00 sec)

example 4_1:

--  Inquiry price is not 160、123 Book information of 
mysql> SELECT * FROM polls_article WHERE price NOT IN (160,123);
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+

example 4_2:

--  Inquire about id yes 1 or 2 or 3 And the price is 132 or 123 Book information of 
mysql> SELECT * FROM polls_article WHERE id IN (1,2,3) AND price IN (132,123);
+----+----------+----------------+-------+--------+----------------------------+
| id | title    | content        | price | author | create_time                |
+----+----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms  |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
+----+----------+----------------+-------+--------+----------------------------+
1 row in set (0.00 sec)

notes :
1、IN Operator general ratio OR Operators perform faster

2、IN The greatest advantage of operators is that they can contain other SELECT sentence , Make it possible to build more dynamically WHERE Clause

3、 In more cases IN The value of the list item is ambiguous , It may be obtained through a sub query , Then take the query result as IN List items to achieve the final query results , Note that the result returned in the subquery must be a field list item .

4、IN List items not only support numbers , It also supports characters and even time and date types , And these different types of data items can be mixed and arranged

 

belt BETWEEN AND The scope of inquiry

1、BETWEEN AND Used to query the value in a certain range , This operator requires two arguments , That is, the start value and end value of the range
    ⑴ If the data field value meets the specified range query criteria , Then the data will be returned ( Contains two boundary values )

2、BETWEEN AND Operators can also be preceded by keywords NOT, Represents a value outside the specified range . If the field value does not meet the value within the specified range , Then the data will be returned

example 5:

--  The inquiry price is in 130 To 140 Data between 
mysql> SELECT * FROM polls_article WHERE price BETWEEN 130 AND 140;
+----+----------+----------------+-------+--------+----------------------------+
| id | title    | content        | price | author | create_time                |
+----+----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms  |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  4 |  Water margin    |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
+----+----------+----------------+-------+--------+----------------------------+

example 5_1:

--  Query data whose creation time is not between two time periods 
mysql> SELECT * FROM polls_article WHERE create_time NOT BETWEEN '2020-10-13 17:08:50.493488' AND '2020-10-13 17:19:18.121264';
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
4 rows in set (0.00 sec)

 

belt LIKE Character matching query for

LIKE The operator

1、 All of the operators described above filter for known values , But this filtering method is not always easy to use .
    ⑴ for example , How to search for text in product name anvil All products of ? It is certainly not possible to use simple comparison operators , At this point, you must use wildcards , Use wildcards to create search patterns that compare specific data
    ⑵ In this case , You can construct a wildcard search pattern , Find out where in the product anvil Products

2、 wildcard : A special character used to match a part of a value . In fact, it is SQL Of WHERE A character with a special meaning in a clause
    ⑴ You can talk to LIKE Wildcards used together are '%' and '_'

3、 search mode : Literal value , Wildcards or a combination of both

4、 To use wildcards in search clauses , Must use LIKE The operator .LIKE instructions MYSQL The following search patterns are compared using wildcard matching instead of direct equal matching

5、MySQL Allows you to NOT Operators and LIKE Operator combinations , To find a string that does not match a particular pattern


Percent sign (%) wildcard

1、 effect : Match characters of any length , Even zero characters

2、 When searching for a match , wildcard "%" It can be placed in different places ( Different positions mean different things )

example 6:

--  Query all title Field to " 3、 ... and " Initial data 
--  Indicates that any will be retrieved with " 3、 ... and " The opening word ,% tell MYSQL Accept " 3、 ... and " Then there is any ( Number ) character 
mysql> SELECT * FROM polls_article WHERE title LIKE ' 3、 ... and %';
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
2 rows in set (0.00 sec)

example 6_1:

--  Query all title Field to "d" Data at the end 
mysql> SELECT * FROM polls_article WHERE title LIKE '%d';
+----+-------+---------+-------+--------+----------------------------+
| id | title | content | price | author | create_time                |
+----+-------+---------+-------+--------+----------------------------+
|  7 | wed   | dwd     |   123 | 32     | 2020-10-13 17:08:50.493488 |
+----+-------+---------+-------+--------+----------------------------+
1 row in set (0.00 sec)

example 6_2:

--  Query all title The field value contains "ds" The data of , No matter "ds" Before and after 、 How many characters are there after 
mysql> SELECT * FROM polls_article WHERE title LIKE '%ds%';
+----+-------+---------+-------+--------+----------------------------+
| id | title | content | price | author | create_time                |
+----+-------+---------+-------+--------+----------------------------+
|  8 | ds    | dwd     |   123 | 32     | 2020-10-13 17:19:18.121264 |
+----+-------+---------+-------+--------+----------------------------+
1 row in set (0.00 sec)

example 6_3:

--  Query all title The field value is marked with "w" start , With "d" Data at the end 
mysql> SELECT * FROM polls_article WHERE title LIKE 'w%d';
+----+-------+---------+-------+--------+----------------------------+
| id | title | content | price | author | create_time                |
+----+-------+---------+-------+--------+----------------------------+
|  7 | wed   | dwd     |   123 | 32     | 2020-10-13 17:08:50.493488 |
+----+-------+---------+-------+--------+----------------------------+
1 row in set (0.00 sec)


Underline (_) wildcard

1、 Another wildcard is the underscore (_). The purpose of underlining is related to % equally , But underscores match only a single character, not multiple characters ( Only the underscores are inconsistent )
    ⑴% wildcard : Match any number of characters
    ⑵_ wildcard : Only any single character can be matched

2、 Underscore wildcard if you want to match more than one character , You need to use the same number of "_"

example 7: Looking to T Begin with m Ending value

mysql> SELECT 
    employeeNumber, lastName, firstName
FROM
    employees
WHERE
    firstname LIKE 'T_m';
+----------------+----------+-----------+
| employeeNumber | lastName | firstName |
+----------------+----------+-----------+
|           1619 | King     | Tom       |
+----------------+----------+-----------+

example 7_1: Looking to b_n Initial value

mysql> SELECT platformName,channelName  FROM roleinfo WHERE channelName LIKE "b_n%";
+---------------+---------------+
| platformName  | channelName   |
+---------------+---------------+
| cinjigame     | binjigame     |
| ainjigame_app | bonjigame_bpp |
+---------------+---------------+

notes :
1、 percentage (%) Wildcards allow zero or more characters to match any string

2、 Underline (_) Wildcards allow you to match any single character

3、 Using underscores (_) Wildcards also need to be used % To specify the location

 

Query null value

1、 When the table is created , The table designer can specify whether the columns in it can contain no values . When a column does not contain a value , Say it contains null values NULL

2、NULL: No value , It is contained with the field 0、 Empty string is different . It is a special value

3、 have access to IS  NULL Clause to check whether the table has NULL value . or IS NOT NULL To check if it is not NULL

example 8:

--  Inquire about author The field values for null The data of 
mysql> SELECT * FROM polls_article WHERE author IS null;
+----+--------+----------+-------+--------+----------------------------+
| id | title  | content  | price | author | create_time                |
+----+--------+----------+-------+--------+----------------------------+
|  9 |  Journey to the west  |  To obtain buddhist scriptures  |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+--------+----------+-------+--------+----------------------------+
1 row in set (0.00 sec)

example 8_1:

--  Inquire about author The field value is not null The data of 
mysql> SELECT * FROM polls_article WHERE author IS NOT NULL;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
+----+-----------+----------------+-------+--------+----------------------------+
6 rows in set (0.00 sec)

 

Logical query criteria

stay SQL You can use logical operators in AND and OR:AND Express " And ",OR Express " or "

belt AND Multi criteria query of

1、 Use SELECT Statement query , You can add query restrictions , This can make the results of the query more accurate

2、 stay WHERE Used in clauses AND The operator specifies that only data that meets all query conditions will be returned ( The query criteria are " And " The relationship between )

3、 have access to AND Connect two or more query criteria , Multiple conditional expressions time use AND Separate

example 9:

--  The inquiry price is greater than 130 And it's not equal to 154 Book information of 
mysql> SELECT * FROM polls_article WHERE price > 130 AND price != 154;
+----+----------+----------------+-------+--------+----------------------------+
| id | title    | content        | price | author | create_time                |
+----+----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms  |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  4 |  Water margin    |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west    |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+----------+----------------+-------+--------+----------------------------+
3 rows in set (0.00 sec)

example 9_1:

--  The inquiry price is greater than 130 And the author is ' Cao xueqin ' Book information of 
mysql> SELECT * FROM polls_article WHERE price > 130 AND author = ' Cao xueqin ';
+----+--------+----------------+-------+--------+----------------------------+
| id | title  | content        | price | author | create_time                |
+----+--------+----------------+-------+--------+----------------------------+
|  3 |  A dream of red mansions  |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
+----+--------+----------------+-------+--------+----------------------------+
1 row in set (0.00 sec)

 

belt OR Multi criteria query of

1、 And AND contrary , stay WHERE Used in clauses OR The operator , It means that data that only needs to meet one of the conditions can be returned ( The query criteria are " or " The relationship between )

2、 have access to OR Connect two or more query criteria , Multiple conditional expressions time use OR Separate

example 10:

--  The inquiry price is greater than 140 Or the author is ' Wu chengen ' Book information of 
mysql> SELECT * FROM polls_article WHERE price > 140 OR author=' Wu chengen ';
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
4 rows in set (0.00 sec)

example 10_1:

--  The inquiry price is 154 or 140 Book information of 
-- IN Operator and OR Operators have the same effect 
mysql> SELECT * FROM polls_article WHERE price =154 OR price = 140;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
3 rows in set (0.00 sec)


AND Operation parent and OR The priority of the operator

1、 Both exist in the filter conditions and and or When the operator , It will be calculated first. AND" And " Calculate again OR" or "( First and then or )

2、 When the calculation priority needs to be specified , You can use parentheses to mark

example 11:

mysql>  SELECT * FROM polls_article WHERE price=123 OR price=154 OR price=140 AND AUTHOR !=' Luo Guanzhong ';
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
+----+-----------+----------------+-------+--------+----------------------------+
5 rows in set (0.00 sec)

example 11_1:

mysql> SELECT * FROM polls_article WHERE (price=123 OR price=154 OR price=140) AND AUTHOR !=' Luo Guanzhong ';
+----+--------+----------------+-------+--------+----------------------------+
| id | title  | content        | price | author | create_time                |
+----+--------+----------------+-------+--------+----------------------------+
|  3 |  A dream of red mansions  |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin  |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  7 | wed    | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds     | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
+----+--------+----------------+-------+--------+----------------------------+
4 rows in set (0.00 sec)

notes :
1、SQL( Like most languages ), Processing OR Operators will take precedence before AND The operator , namely AND Operators have a higher priority . So the meaning of the above two examples is completely different

2、 The solution to this problem is to use parentheses to explicitly group the corresponding operators ,DBMS The filter conditions in parentheses will be processed first

3、 Use at any time with AND and OR Operator WHERE When clause , You should use parentheses to explicitly group operators , Don't rely too much on the default calculation order

 

No duplicate query results

1、 grammar :SELECT DISTINCT X  FROM A;

2、 effect : Returns rows in a column that do not have values ( In fact, the query is also based on columns , There is only one filter condition -- Data in the same column are compared with each other )

3、 Parameters :
    ⑴DISTINCT:SQL keyword , Indicates that only different values are returned
    ⑵X: Columns to be retrieved
    ⑶A: From which table to retrieve

example 12:

--  Inquire about polls_article In the table price Field and price The field values are not repeated 
mysql> SELECT DISTINCT price FROM polls_article;
+-------+
| price |
+-------+
|   132 |
|   154 |
|   140 |
|   123 |
|   345 |
+-------+
5 rows in set (0.00 sec)

example 12_1:

mysql> SELECT DISTINCT price FROM polls_article WHERE price > 130;
+-------+
| price |
+-------+
|   132 |
|   154 |
|   140 |
|   345 |
+-------+
4 rows in set (0.00 sec)

  example 12_2:

mysql> SELECT DISTINCT s.`s_id`,s.`s_name`,s.`s_name`,s.`s_telephone` FROM suppliers s LEFT JOIN fruits f ON F.`s_id`=s.`s_id` WHERE F.`f_price` >= 120;
+------+----------------+----------------+-------------+
| s_id | s_name         | s_name         | s_telephone |
+------+----------------+----------------+-------------+
|  102 |  The first fruit merchant in East China  |  The first fruit merchant in East China  | 12345678911 |
|  103 |  The first fruit merchant in Northwest China  |  The first fruit merchant in Northwest China  | 98765432101 |
+------+----------------+----------------+-------------+
2 rows in set (0.00 sec)

notes :

1、 have access to DISTINCT Keyword de duplication of query results , When removing duplicates, you can use a field , Or multiple fields

2、 Use DISTINCT Keyword de duplication of query results ,DISTINCT Keywords can only be used at the top of all query columns , Indicates that all the following fields are combined to remove duplicates ( Remove weight as a whole with one behavior : The first field must not be duplicated , The second field is not duplicated )

 

Sort query results

1、 grammar :SELECT Name FROM Table name ORDER BY Name ;

2、 effect : Sorting data , The data to be queried will be sorted according to the specified conditions
    ⑴ When using SELECT FROM when , If you don't sort , The data will generally be displayed in the order it appears in the underlying table . This can be the order in which the data was originally added to the table or something else
    ⑵ When sorting data : You can only specify which columns to sort by , Cannot specify collation ( Only the built-in collation can be used : In ascending or descending order )

3、ORDER BY: instructions MySQL Sorts data alphabetically or numerically in a specified column ( With BY The column of the following column name is the benchmark )
    ⑴ The default sort method is : Sort in ascending alphabetical or numerical order

 

Single column sort

1、 namely , Sort according to a column in the query results

example 13:

--  according to price Sort by field value ( Default ascending order )
mysql> SELECT * FROM polls_article WHERE  price > 130 ORDER BY price;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
5 rows in set (0.00 sec)

example 13_1:

--  according to title Sort by field value ( Default ascending order )
mysql>  SELECT * FROM polls_article WHERE  price > 130 ORDER BY title;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
5 rows in set (0.00 sec)


Multi column sorting

1、 effect : Sort by multiple criteria

2、 grammar :SELECT Name 1, Name 2, Name 3 FROM Table name ORDER BY Name 1, Name 2;

example 14:

mysql> SELECT * FROM polls_article ORDER BY price,id;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
7 rows in set (0.00 sec)

notes : In the example above BY Then there are two parameters
1、 You mean for : If the first parameter (price) Press the second parameter only when multiple rows have the same value (id) Sort

2、 If the value of the first parameter is unique , Then it will not be sorted by the second parameter


Specify sorting direction

1、 effect : Specifies whether the sort is ascending or descending

2、 grammar :SELECT Name 1, Name 2 FROM Table name ORDER BY Name DESC;

3、 Parameters :
    ⑴DESC:SQL keyword , Indicates descending sort ,ASCII Ascending sort is the default sort direction
    ⑵DESC Keywords only apply to column names directly before them ( That is to say ORDER BY Back ,DESC The previous column names will work ), If you want to sort multiple columns in descending order , You must add... After the column name of each column DESC keyword
    ⑶ And DESC The opposite keyword is ASC, however ASC Not much use , Because ascending is the default  

example 15:

mysql> SELECT * FROM polls_article WHERE  price > 130 ORDER BY price DESC;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
5 rows in set (0.00 sec)

example 15_1:

-- DESC The keyword only applies to the column name that precedes it 
mysql> SELECT * FROM polls_article ORDER BY price DESC,id;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
+----+-----------+----------------+-------+--------+----------------------------+
7 rows in set (0.00 sec)

mysql> SELECT * FROM polls_article ORDER BY price,id DESC;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
7 rows in set (0.00 sec)

 

Limit the number of query results

1、SELECT grammar :SELECT X FROM A LIMIT num;

2、 effect : Limit the number of data returned by the query
    ⑴ Sometimes you only need to return the latest one of all the data that meets the conditions , Then you can sort by time , Then return the first data , You don't have to return all of them

3、LIMIT Keyword Syntax :LIMIT [ Position offset ], Row number
    ⑴ Position offset : Express MYSQL From which line does the calculation begin , Is an optional parameter , If you don't specify , It will start with the first data in the table ( The default offset is 0)
    ⑵ Row number : Required parameters , Indicates the number of data pieces returned

notes :
1、 The position offset of the first data is 0, The position offset of the second data is 1... And so on

2、LIMIT Accept one or two numeric parameters
    ⑴ Argument must be an integer constant
    ⑵ If two parameters are given , The first parameter specifies the offset of the return record line , That is to say, starting from the number of ; The second parameter specifies the maximum number of rows to return records
    ⑶ The offset of the initial record line is 0( instead of 1)

3、 With one parameter LIMIT Always start with the first line ; Specifies when the number of rows to retrieve is greater than the maximum number of rows , All rows are returned
    ⑴ namely ,LIMIT n Equivalent to LIMIT 0,n

example 16:

mysql> SELECT * FROM polls_article;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
7 rows in set (0.00 sec)

--  There is only one parameter : Said to return to " Row number ", The default position offset is 0, Then the first data is retrieved 1 Row data ( That is, the first data )
mysql> SELECT * FROM polls_article LIMIT 1;
+----+----------+----------------+-------+--------+----------------------------+
| id | title    | content        | price | author | create_time                |
+----+----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms  |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
+----+----------+----------------+-------+--------+----------------------------+
1 row in set (0.00 sec)


example 16_1:

mysql> SELECT * FROM polls_article;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  7 | wed       | dwd            |   123 | 44     | 2020-10-13 17:08:50.493488 |
|  8 | ds        | dwd            |   123 | 32     | 2020-10-13 17:19:18.121264 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
7 rows in set (0.00 sec)

--  There is only one parameter : Said to return to " Row number ", The default position offset is 0, Then the first data is retrieved 3 Row data 
mysql>  SELECT * FROM polls_article LIMIT 3;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
3 rows in set (0.00 sec)


example 16_2:

mysql> SELECT * FROM polls_article WHERE  price > 130 ORDER BY price;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  1 |  The romance of The Three Kingdoms   |  Monkey king made a scene in heaven  |   132 |  Wu chengen  | 2020-09-13 14:48:30.000000 |
|  4 |  Water margin     |  to join rebels        |   140 |  Shi Naian  | 2020-09-13 12:48:30.962070 |
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
|  3 |  A dream of red mansions     |  Grandma Liu enters the Grand View Garden  |   154 |  Cao xueqin  | 2020-09-13 12:48:30.962070 |
|  9 |  Journey to the west     |  To obtain buddhist scriptures        |   345 | NULL   | 2020-11-14 15:48:59.000000 |
+----+-----------+----------------+-------+--------+----------------------------+
5 rows in set (0.00 sec)

--  There are two parameters : The position offset is 2, Indicates starting from the third line ( Include the third line )," Row number " by 1, Indicates that a row of data is returned 
mysql> SELECT * FROM polls_article WHERE  price > 130 ORDER BY price LIMIT 2,1;
+----+-----------+----------------+-------+--------+----------------------------+
| id | title     | content        | price | author | create_time                |
+----+-----------+----------------+-------+--------+----------------------------+
|  2 |  The romance of The Three Kingdoms 1 |  At the end of the Eastern Han Dynasty, there were three kingdoms  |   154 |  Luo Guanzhong  | 2020-09-13 12:48:30.962070 |
+----+-----------+----------------+-------+--------+----------------------------+
1 row in set (0.00 sec)

 

原网站

版权声明
本文为[Cat fearless mouse a]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021422096341.html