05 MySQL's query, insert, update and delete

01- query data

Syntax: 
select * | field list from Table 1, Table 2 where the HAVING expression ... ... by the Order Group by ... limit .. 

# query all the fields in 
select * from table name; 

# query a single field 
select column name from name; 

# query multiple fields 
select a column name, column name 2, ... from the table name; 

# query specified recording 
select column names from table where query; 

# with IN keyword query 
select s_id, f_name , f_price from Fruits WHERE s_id the iN (100, 110 ); 

# the nOT keyword search condition is not within the range of conditions. 
s_id SELECT, f_name, f_price from Fruits the NOT WHERE s_id the IN (100, 110); 

# BETWEEN and 
SELECT s_id, f_name, f_price from Fruits WHERE s_id BETWEEN 100 and 110 ; 

# like 
SELECT F_ID, f_name from Fruits WHERE f_name like ' B% ' ;
 % : a plurality of matching characters 
_: matches any single character 

# query null 
SELECT F_ID, f_name from Fruits f_name WHERE iS null; 

# with many conditions and the 
SELECT F_ID, f_name from Fruits WHERE F_ID = ' 100 '  and f_name = ' Apple ' ;

# Many conditions with OR query 
select F_ID, f_name from Fruits WHERE s_id = 100 or s_id = 101 ; 

# query results to a weight 
select distinct field name from name; 

# order query results 
select field names from table order by field name ; 

# multiple column sort 
select column names from table order by field name 1, field name 2; 

# specify the sort direction DESC descending, ASC ASC 
select column names from table order by field name 1, field name 2 DESC; 

# packet query 
Conditional expression field HAVING GROUP bY 

# in the group by clause WITH ROLLUP 
add a record the query packet after the recording, the recording of all record query calculates the sum, i.e., the number of statistical records. 
Example: GROUP BY s_id WITH ROLLUP

# Using GROUP BY and ORDER BY together 

# quantity limit query results using LIMIT 
LIMIT the number of rows

Queries using aggregate functions

 

 Join query

# Join queries the INNER JOIN 
select a column name, column name 2, ... from Table 1 inner join Table 1 ON condition; 

# outer join query 
left connecting LEFT JOIN: Back left table and the right table records all connection comprises equal field records. 
Right connection RIGHT JOIN: Returns all records including records left table and the right table in connection equivalent field. 

# Complex join query conditions

Subqueries

# The ANY 
long num1 is greater than any value num2, that match the query 
the SELECT num1 from TB1 the WHERE num1> the ANY (the SELECT num2 from TB2); 

# ALL 
num1 than num2 all values greater value, that match the query 
the SELECT num1 from TB1 num1 WHERE> ALL (SELECT num2 from TB2); 

# EXISTS 
behind EXISTS keyword parameter is an arbitrary sub-query, subquery is evaluated to determine whether it returns rows returned if at least one row, the result of EXISTS is true In this case the outer query, the query would be, on the contrary, the result is false, the outer statement at this time will not be queried. 

# Subband IN query keywords 
when a keyword IN subquery, the inner query returns only one column of data, the data column will provide the value is compared to the outer query operation.

Merge query data UNION

UNION using keywords, a plurality of SELECT statements can be given, and combinations thereof results into a single result set. 
When combined, the two tables corresponding to the number of columns and data types must be the same. Use the keyword UNION or UNION ALL SELECT divided between the various statements. 
UNION ALL is not used, when executed remove duplicate records, returns all rows are unique; use the keyword ALL role is not to delete duplicate rows are not automatically sort the results. 
The basic syntax is as follows: 
the SELECT column, ... the FROM table1 
the UNION [ALL] 
the SELECT column, the FROM ... Table2;

Alias ​​table and fields

# Table name alias 
table name AS alias table 

# field name alias 
column name alias AS column

Using regular expressions inquiry

MySQL REGEXP keyword specifies the character used to match the regular expression pattern.

 

 

# Query record begin with a specific character or string ^ 
 SELECT * from Fruits WHERE f_name REGEXP, ' ^ B ' ; 

# query to a particular record or character strings ending 
SELECT * from Fruits WHERE f_name regexp ' Y $ ' ; 

# symbol '' instead of an arbitrary character string in a 
SELECT * from Fruits WHERE f_name regexp ' AG ' ; 

# use * and + to match multiple characters 
   * matches any number of times, including zero. + Matches the preceding character at least once. 

# Matches the specified string 
just to your query string, such as a plurality of strings to be matched between the delimiters | . 
the SELECT * from Fruits regexp the WHERE f_name' ON | AP ' ; 

# matches any one of the specified character in 
brackets [] to brake a set of characters, match any one character only, the text that is looking for.
# Record lookup f_name field contains the letters o or t 
SELECT * from Fruits WHERE f_name regexp ' [OT] ' ; 

# matching characters other than the specified character 
[^ set of characters] matches any character set is not specified. 
F_name record query field contains the letters a ~ e and a number other than 1 to 2 characters 
SELECT * from Fruits WHERE F_ID regexp ' [A-E1-2] ' ; 

# used {n,} or {n, m} specified character the number of consecutive string of 
{n,} represents at least n times matches the preceding character; {n, m} indicates that the character string matches less than n times, not more than m times. 
the SELECT * from Fruits regexp the WHERE f_name'x{2,}';

02- insert data

2.1 for all fields in the table of insert data

INSERT INTO table_name (column_list) VALUES ( values_list); 
Note: column number of fields and data values must be the same when using the statement. 
1 , there are two methods to the table all the fields interpolation: one is to specify all of the field names, and the other is completely specified field name.
2 , the order of the column name of the INSERT statement behind the order table is not defined. That is, when inserting data, do not require the insertion of defined sequence table, in the same order as long as the guaranteed value of the column can be field.
3, when INSERT a data column name list is empty to allow this case, the list needs to specify a value for each field in the table, the order of weapons and human values must define the order of the data block in the same table.

2.2 insert data for the specified field of the table

Insert as specified field data table, the value is inserted into only a portion of the INSERT statement in the field, and other fields of default values ​​defined for the table.

2.3 simultaneously insert multiple records

INSERT statement can be simultaneously inserted into a plurality of data records in the table, a list of a plurality of specified value insertion, separated by a comma between each value list, the following basic syntax: 
INSERT the INTO table_name (the column_list) the VALUES (values_list1), (values_list2) , ...;

2.4 query results into a table

The basic syntax is as follows: 
the INSERT the INTO table_name1 (column_list1) 
the SELECT (column_list2) the FROM table_name2 the WHERE (for condition Condition); 
Note: the same number of fields column_list1 column_list2 list, the same data type.

03- update data

# Basic syntax structure is as follows: 
the UPDATE table_name the SET column_name1 = VALUE1, column_name2 value2 =, ..., = column_namen valueN the WHERE (for condition Condition); 

Example: 
the UPDATE the SET Person Age = 15, name = ' Hyp ' the WHERE ID = . 11 ;

04- delete data

# The basic syntax is as follows: 
the DELETE the FROM table_name [the WHERE <for condition Condition> ]; 

Example: the DELETE the FROM person WHERE ID = 10 ; 

# delete multiple records 
the DELETE the FROM person the WHERE Age BETWEEN. 19 and 22 is ; 

# delete the person table all the data, but The table also exist 
DELETE FROM person;

 

Guess you like

Origin www.cnblogs.com/pgxpython/p/11725708.html