mysql query statement select multiple usage

1. Use select to sort by keywords

select syntax format sorted by keywords

SELECT column1 (field 1), column2,... FROM table name ORDER BY column1, column2,... ASC|DESC;
1) Combine ORDER BY statement to achieve sorting
2) Sorting can be based on one or more fields
3) ASC: Ascending order , Mysql default arrangement method, the system defaults ascending order if the arrangement method is not declared
4)DESC: descending order

Example operation:
  • View the default sorting method of system tables
mysql> select * from stu;    //查询数据库中stu表中的系统默认按主键升序排列方式
+----+---------+--------+-------+
| id | name    | score  | hoby |       //可以看到这个表由4个字段和六条记录组成
+----+---------+--------+-------+
|  1 | tianxia | 78.00 |    2 |
|  2 | diyi    | 89.00 |    1 |
|  3 | wode    | 55.00 |    1 |
|  4 | tade    | 57.00 |    3 |
|  5 | nida    | 66.00 |    1 |
|  6 | liushou | 55.00 |    3 |
+----+---------+-------+------+
6 rows in set (0.00 sec)
  • Change the table to sort in descending order based on id
mysql> select * from stu order by id desc;
+----+---------+--------+-------+
| id | name    | score  | hoby |
+----+---------+--------+-------+
|  6 | liushou | 55.00 |    3 |
|  5 | nida    | 66.00 |    1 |
|  4 | tade    | 57.00 |    3 |
|  3 | wode    | 55.00 |    1 |
|  2 | diyi    | 89.00 |    1 |
|  1 | tianxia | 78.00 |    2 |
+----+---------+-------+------+
6 rows in set (0.00 sec)
  • Change the table to sort in descending order based on the score field first, and sort it based on the hoby field.
mysql> select * from stu order by score desc,hoby desc;
+----+---------+-------+------+
| id | name    | score | hoby |
+----+---------+-------+------+
|  2 | diyi    | 89.00 |    1 |
|  1 | tianxia | 78.00 |    2 |
|  5 | nida    | 66.00 |    1 |
|  4 | tade    | 57.00 |    3 |
|  6 | liushou | 55.00 |    3 |
|  3 | wode    | 55.00 |    1 |
+----+---------+-------+------+
6 rows in set (0.00 sec)

Insert picture description here

Two, use select to group the results

select syntax format for grouping results

SELECT column, function FROM table name WHERE condition GROUP BY field;
1) Use GROUP BY statement to achieve grouping
2) Can be used in conjunction with functions, usually combined with aggregate function count (field name)
3) Results can be calculated by one or more fields Row grouping

Example operation
  • The number of people in each group in the hoby field of the statistics table
mysql> select count(name),hoby from stu where hoby >= 1 group by hoby;
+-------------+------+
| count(name) | hoby |
+-------------+------+
|           3 |    1 |
|           1 |    2 |
|           2 |    3 |
+-------------+------+
3 rows in set (0.00 sec)
  • Count the number of people in each group in the hoby field of the table, and sort the count (name) in descending order.
mysql> select count(name),hoby from stu group by hoby order by count(name) desc;
+-------------+------+
| count(name) | hoby |
+-------------+------+
|           3 |    1 |
|           2 |    3 |
|           1 |    2 |
+-------------+------+
3 rows in set (0.00 sec)

Three, use select query to limit item results

Use select query to limit the grammatical structure of entry results

SELECT column1, column2,... FROM table name LIMIT [offset,] number;
1) [offset,] position offset, starting from 0. Brackets can be omitted in the grammatical structure.
2) number returns the maximum number of record rows.
3) Starting from the offset position row, query number rows down.
4) The LIMIT statement is used to restrict reading records.

Example operation
  • The first three rows of the lookup table
mysql> select * from stu limit 0,3;
+----+---------+-------+------+
| id | name    | score | hoby |
+----+---------+-------+------+
|  1 | tianxia | 78.00 |    2 |
|  2 | diyi    | 89.00 |    1 |
|  3 | wode    | 55.00 |    1 |
+----+---------+-------+------+
3 rows in set (0.00 sec)
  • 4 to 6 rows of the lookup table
mysql> select * from stu limit 3,3;
+----+---------+-------+------+
| id | name    | score | hoby |
+----+---------+-------+------+
|  4 | tade    | 57.00 |    3 |
|  5 | nida    | 66.00 |    1 |
|  6 | liushou | 55.00 |    3 |
+----+---------+-------+------+
3 rows in set (0.00 sec)

Four, use select to set alias

Use select to set the syntax structure of the alias

SELECT column (field name) AS column (field name) FROM table name

  1. You can set aliases for fields and tables
  2. When using the AS statement to set the alias, the keyword AS can be omitted
  3. When setting aliases, ensure that they cannot conflict with other table or field names in the library.
    4) AS can be used as a connection statement in addition to setting aliases
Example operation
  • Set alias statistics for the field to
    count the number of field records mysql> select count(name) as a from stu;
    ±–+
    | a |
    ±–+
    | 6 |
    ±–+
    1 row in set (0.00 sec)
  • Set the alias for the table to view the id of the first three rows
    mysql> select c.id from stu as c limit 3;
    ±—+
    | id |
    ±—+
    | 1 |
    | 2 |
    | 3 |
    ±—+
    3 rows in set (0.00 sec)
  • Use AS as the connection statement
    Import the data in the stu table into the new table through the AS connection statement
    mysql> create table xuexi as select * from stu where score >= 80;
    Query OK, 1 row affected (0.00 sec)
    Records: 1 Duplicates: 0 Warnings: 0
mysql> desc stu;
+-------+--------------+------+-----+---------+----------------+
| Field | Type              | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(5)               | NO   | PRI | NULL    | auto_increment |
| name  | char(15)     | NO   |         | NULL    |                |
| score | decimal(5,2) | YES  |      | NULL    |                |
| hoby  | int(5)             | YES  |      | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> desc xuexi;
+-------+--------------+------+-----+---------+-------+
| Field | Type               | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(4)               | YES  |          | 0  |                |
| name  | char(15)      | NO   |         | NULL    |       |
| score | decimal(5,2) | YES  |        | NULL    |       |
| hoby  | int(5)             | YES  |        | NULL    |       |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

AS actually defines the table structure of the new table and the connection table exactly the same, and imports the data of the connection table into the new table,
but some constraint types of the new table are not defined in the same way as the connection table, for example, key Primary key, Default default value, Extra extended attributes, etc.

Five, use select commonly used wildcards

Commonly used wildcards

SELECT column (field name) FROM table name WHERE column LIKE'_%'

  • % Means zero, one or more
  • _ Indicates a single character
    1) usually used for fuzzy query, not clear about the specifics, you can use it when you only know the general
    2) used to indicate some characters in the field
    3) usually used in conjunction with like, and complete the query with WHERE
Example operation
  • The usage of wildcard character'%'
    Query the records starting with w in the name field of the stu table
mysql> select * from stu where name like 'w%';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
|  3 | wode | 55.00 |    1 |
+----+------+-------+------+
1 row in set (0.00 sec)
  • The usage of the wildcard character'_'
    Query the records in the name field of the stu table that are preceded by two arbitrary characters and end with de
mysql> select * from stu where name like '__de';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
|  3 | wode | 55.00 |    1 |
|  4 | tade | 57.00 |    3 |
+----+------+-------+------+
2 rows in set (0.00 sec)
  • The combined usage of the wildcard character'_%'
    Query the record in the name field of the stu table that is preceded by an arbitrary character with an i character in the middle and ends with de
mysql> select * from stu where name like '_i%';
+----+---------+-------+------+
| id | name    | score | hoby |
+----+---------+-------+------+
|  1 | tianxia | 78.00 |    2 |
|  2 | diyi    | 89.00 |    1 |
|  5 | nida    | 66.00 |    1 |
|  6 | liushou | 55.00 |    3 |
+----+---------+-------+------+
4 rows in set (0.00 sec)

Six, use select subquery

Use the common syntax structure of select subquery

SELECT column (field) FROM table name WHERE column IN (SELECT column FROM table name WHERE column IN (…));
1) Subquery is also called inner query or nested query
2) is executed before the main query, and the result will be As the condition of the outer main query
3) Sub-queries can be used in addition, deletion, modification and search.
4) Multi-layer nesting is supported.
5) The reading method is to read from the inner layer to the outer layer.
6) The IN statement is used to judge a certain Whether the value is in the given result set
7) The field type in the subquery must be the same as the field type of the main query
8) When the result set is used as the table of the subquery, it cannot be used directly, and an alias must be created for the result set

Example operation
  • Based on table subquery
    Query zhi table
    mysql> select * from zhi;
    ±-----+
    | id |
    ±-----+
    | 1 |
    | 3 |
    | 4 |
    | 5 |
    ±-----+
    4 rows in set (0.01 sec)
    query stu table
mysql> select * from stu;
+----+---------+-------+------+
| id | name    | score | hoby |
+----+---------+-------+------+
|  1 | tianxia | 78.00 |    2 |
|  2 | diyi    | 89.00 |    1 |
|  3 | wode    | 55.00 |    1 |
|  4 | tade    | 57.00 |    3 |
|  5 | nida    | 66.00 |    1 |
|  6 | liushou | 55.00 |    3 |
+----+---------+-------+------+
6 rows in set (0.01 sec)

Use the id value of the zhi table as the id query condition of the stu table to query database records

mysql> select * from stu where id in (select id from zhi);
+----+---------+-------+------+
| id | name    | score | hoby |
+----+---------+-------+------+
|  1 | tianxia | 78.00 |    2 |
|  3 | wode    | 55.00 |    1 |
|  4 | tade    | 57.00 |    3 |
|  5 | nida    | 66.00 |    1 |
+----+---------+-------+------+
4 rows in set (0.00 sec)
  • Based on the result set subquery
    Query the records of the two fields in the stu table
    mysql> select id,score from stu;
    ±—±------+
    | id | score |
    ±—±------+
    | 1 | 78.00 |
    | 2 | 89.00 |
    | 3 | 55.00 |
    | 4 | 57.00 |
    | 5 | 66.00 |
    | 6 | 55.00 |
    ±—±------+
    6 rows in set (0.00 sec)
    will find When the result set of is used as a subquery statement, an alias must be set
    mysql> select a.id from (select id,name from stu) a;
    ±—+
    | id |
    ±—+
    | 1 |
    | 2 |
    | 3 |
    | 4 |
    | 5 |
    | 6 |
    ±—+
    6 rows in set (0.00 sec)

Seven, use the regular expression query supported by select

Use the grammatical structure of regular expression queries supported by select

SELECT column… FROM table name WHERE column REGEXP'Regular expression '
1) According to the specified matching mode to match the special characters that meet the requirements in the record
2) Use the REGEXP keyword to specify the matching mode

Example operation
  • Query records whose name field starts with w
mysql> select * from stu where name regexp '^w';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
|  3 | wode | 55.00 |    1 |
+----+------+-------+------+
1 row in set (0.00 sec)
  • Query the records whose name field ends with de
mysql> select * from stu where name regexp 'de$';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
|  3 | wode | 55.00 |    1 |
|  4 | tade | 57.00 |    3 |
+----+------+-------+------+
2 rows in set (0.00 sec)
  • Query the record whose hoby field is 1
mysql> select * from stu where hoby regexp '1';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
|  2 | diyi | 89.00 |    1 |
|  3 | wode | 55.00 |    1 |
|  5 | nida | 66.00 |    1 |
+----+------+-------+------+
3 rows in set (0.00 sec)

Eight, use select to determine the value of NULL

NUll is worth introducing

1) Value that is definitely missing
2) It means that empty objects occupy space
3) It is different from the number 0 or blank (spaces)
4) Use IS NULL or IS NOT NULL to judge
5) Difference between NUll value and null value

  • The length of the null value is 0, which does not occupy space; the length of NULL value is NULL, which occupies space
  • IS NULL cannot determine the null value
  • Use "=" or "<> (not equal to)" to handle null values
  • COUNT() When calculating, NULL will be ignored and will not be added to the statistics, and empty values ​​will be added to the calculation
Example operation
 mysql> select * from stu;
+----+---------+-------+------+------+
| id | name    | score | hoby | dizi |
+----+---------+-------+------+------+
|  1 | tianxia | 78.00 |    2 | nj   |
|  2 | diyi    | 89.00 |    1 | nj   |
|  3 | wode    | 55.00 |    1 | NULL |
|  4 | tade    | 57.00 |    3 | NULL |
|  5 | nida    | 66.00 |    1 | nj   |
|  6 | liushou | 55.00 |    3 |   |
+----+---------+-------+------+------+
6 rows in set (0.00 sec)
  • Query is NULL worth recording
mysql> select * from stu where dizi is null;
+----+------+-------+------+------+
| id | name | score | hoby | dizi |
+----+------+-------+------+------+
|  3 | wode | 55.00 |    1 | NULL |
|  4 | tade | 57.00 |    3 | NULL |
+----+------+-------+------+------+
2 rows in set (0.00 sec)
  • The query is NOT NULL worth recording
mysql> select * from stu where dizi is not null;
+----+---------+-------+------+------+
| id | name    | score | hoby | dizi |
+----+---------+-------+------+------+
|  1 | tianxia | 78.00 |    2 | nj   |
|  2 | diyi    | 89.00 |    1 | nj   |
|  5 | nida    | 66.00 |    1 | nj   |
|  6 | liushou | 55.00 |    3 |      |
+----+---------+-------+------+------+
4 rows in set (0.00 sec)
  • NULL will not be added to statistics
    mysql> select count(dizi) from stu;
    ±------------+
    | count(dizi) |
    ±------------+
    | 4 |
    ±------------+
    1 row in set (0.00 sec)
    Originally there were six records, but only four were counted, because COUNT() will ignore NULL and will not add Statistics, null values ​​will be added to the calculation

Guess you like

Origin blog.csdn.net/wulimingde/article/details/109074448