The second part of the SQL statement (used in MySQL)

   Add, delete, check, and modify table data

(Foreword, in general, the keywords in the SQL statement need to be capitalized, which is more standardized, but I used lowercase for the sake of recognition)

(Because mysql is case-insensitive, all lowercase will also be executed)

   add data to the table

    insert into table_name (column 1, column 2, column 3,....)

   values("column 1 value","column 2 value","column 3 value",.....);

   It should be noted that sometimes we do not need to write out the column names in particular, because we already know which columns there are and how many columns there are, so we can directly write out the values;

    And for the auto-incrementing column, we can use null instead, so that the value of the column in the row in the data book will automatically enter the value of the column in the previous row plus one; but we can't replace it with an empty string, so An error will be reported because their data types are different. as the picture shows:

insert into ordered values("21","93-12-12",1);
insert into ordered values(null,"93-12-14",2);
insert into ordered values("","93-12-15",3);

   First, the first column is automatically incremented, and then enter the above three statements,

 

  The result of the first sentence is: the result of the

   second line is: the result of the

   third line is:

21:22:43	insert into ordered values("","93-12-15",3)	Error Code: 1366. Incorrect integer value: '' for column 'o_num' at row 1	0.000 sec

 

 

    query table data

    Select the field to be queried from table_name where limit statement [ group by field] [ having <>] [ order by column name desc or] [ limit limit the number of query result rows]

   The above statement where and the following restriction statements are optional. Next, we will explain their usage one by one.

   The first is to query the full table

    select *from table_name; The * symbol is a wildcard representing all fields.

    Query several columns in a table

     select column name,column name,... from table_name; The column name is the field name, the specified field in the query table, the field name can be multiple or one.

     Use where constraints when querying

    select column name,... from table_name where constraints;

    Several ways to restrict conditions can be viewed here.

     

    Query unique results

     select distinct 列名,... from table_name;

 

    Sort the query results according to the column after order by 

     select 列名,... from table_name order by 列名 [desc or asc];

     desc means descending order from top to bottom, asc means ascending order, the default is ascending order.

 

      Group query results by column name

       select 列名,... from table_name  group by 列名;

      Usually the group by statement is used together with the sql function. We will discuss the function problem in detail later.

 

      Use added restrictions after query grouping

      select column name,... from table_name  group by column name having restrictions;

      This statement usually also needs to be used with the sql function.

 

     Add the with rollup keyword after group by

      select 列名,... from table_name  group by 列名 with rollup

      The keyword can be one more line in the query result to represent the sum of the query records.

 

      Group by multiple fields

      select column name,... from table_name   group by column name, column name,...;

      At this time, it will first group by the first column name, and then group by the second column name when the first column name has the same value, and the third and so on.

     Limit the number of query results

     select 列名,... from table_name limit[n,m];

     The value of n is the position offset of the query result (the current number of rows - the value of the first row), that is, if you want to display from the first row, n=0 (ps:1-1); from the second row, n =1,(ps:2-1); in the third line, n=2,(ps:3-1).

     The value of m specifies the number of query results returned (this should be well understood)

 

   Multi-table query

   Generally speaking, a multi-table query requires fields with the same meaning in multiple tables, and the field names may be different

   Join query: ⑴ Inner join query

    select field 1, field 2, field 3, field 4 from table 1 inner join table 2 on table 1. same meaning field name = table 2. same meaning field name;

The field 1234 may belong to Table 1 and Table 2 respectively. In fact, there is another way of writing this statement.

      select field 1, field 2, field 3, field 4 from table 1, table 2 where table 1. same meaning field name = table 2. same meaning field name;

      And why not use the second way of writing is because the first way of writing can clearly point out the connection conditions, and the where clause sometimes affects the query conditions.

 

  (2) Outer join query

   Outer joins are divided into left joins and right joins.

   Left join: Returns all records in the left table and records with equal join fields in the right table,

  For example   , user1 table user2 table
                                     
 executes the left join statement like this: select user1.a,b,c from user1 left outer join user2 on user1.a=user2.a;
 The result is
  that the left outer join will connect the right table The data is inserted into the left table according to the matching of user1.a=user2.a. If there is data in one row in the left table but not in the right table, the data is replaced by null.

 

   The right outer join is this statement,

select user1.a,b,c from user1 right outer join user2 on user1.a=user2.a;

The result is
that because the query field is user1.a, the fourth row of the a field in the result is null, and the right outer join is the reverse of the left outer join.
 
 

      

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326113076&siteId=291194637