The database table query

I. query template

SELECT [ALL | DISTINCT] <field 1> / <expression>, <field 2> 
                                     [AS] <new field name> 
the FROM <table name> [AS] <alias> / sub-queries [AS] <alias> 
[ the WHERE <condition> ]
[The GROUP BY <field> [HAVING <condition> ]]
[The ORDER BY <field> [ASC | DESC]];        
View Code

ALL and DISTINCT

distinct data deduplication
The default is not all heavy
View Code

AS alias

AS alias
View Code

expression

The field type is numeric field may be four arithmetic operation
View Code

 

II. Conditions inquiry

 comparison predicate=, >, <, >=, <=, !=, <>, !>, !<; 
 field = value;Not Not field = value
 range predicates
BETWEEN AND, NOT BETWEEN AND 
 field ( Not ) BETWEEN value. 1 and value 2
 set of predicates
IN, NOT IN 
 field ( Not ) in (value 1, value 2 and value 3)
 character match
LIKE, NOT LIKE % : matches any number of characters
_: match an arbitrary character
 field ( not ) like "string"
 null predicate
IS NULL, IS NOT NULL 
 predicate logic
AND, OR, NOT
 condition. 1 and Condition 2
regexp regular
 conditions for determination, string matching
 Example: Field regexp ' regular expression ' 
# SELECT * from EMP WHERE name regexp '^ J * (n-| Y) $.';
View Code

Screening for conditions

III. Grouping queries

 GROUP BY and aggregate functions

  • GROUP BY query results by the specified column packet, equal to the property value tuples as a row group. Typically the polymerization with the use of the function
  • After the grouping condition is determined for use only HAVING GROUP BY
  • Aggregate Functions
    • max min avg sum count
      • Example: max (field)

HAVING phrase

  • Output groups specified condition
  • Examples having conditions

ps: HAVING general statement is applied after the packet with the function

Four .ORDER BY CONCAT sort of limit

ORDER BY ordering

Specified query result table column value in ascending order (ASC) or descending (desc) sorted in ascending order by default (sc)
Example: order by field desc
View Code

CONCAT与GROUP_CONCAT

concat spliced ​​data without using group by down
Example: select concat (field 1, 'string', 'field 2')
concat later spliced ​​data using group by
The same effect as the concat
View Code

limit

Used to limit the number of pieces, written in the last sentence.
When the limit is only one parameter, the parameter indicates a number of show
time limit has two parameters, the first parameter indicates the starting position, the second parameter represents the starting position after the display strip
select * from emp limit 5,5;
View Code

 

V. multi-table query

  • Query classification table
    • United-table query
    • Subqueries
  • The connector (inner join)
    • Take two recording correspondence relationship table
    • Table 1 inner join select Table 2 on Table 1. Table 2 Field field =
  • Left connection (left join)
    • Including on the basis of connections reserved table left no record of correspondence between
    • Table 1 left join select Table 2 on Table 1. Table 2 Field field =
  • Right connection (right join)
    • On the basis of the inner connector, keep a record of the table do not correspond to the right
    • Table 1 right join select Table 2 on Table 1. Table 2 Field field =
  • Full connection (union)
    • Reserved inner base connected to the left, to the right of the table do not correspond to recording (that is connected to the left plus right connection remove duplicates)
    • 1 left join select Table 2 on Table 1. Table 1 Field 1 Field Union = Table 2. Table 1 right join select Table 2 on Table 1. Table 2 Field field =
  • Subqueries
    • Query results as a table of another query a sql statement

 

VI. Strict Mode

Usage: After Mysql uses packet to obtain additional information not being given field

Setting mode

  • Variables like Show '%% MODE';  # sql_mode value of fuzzy matching See
  • set session currently active window
  • set global globally valid
  • = sql_mode, Ltd. Free Join the SET "STRICT_TRANS_TABLES, only_full_group_by"; # amended as strict mode

 

Guess you like

Origin www.cnblogs.com/Cpsyche/p/11390741.html