MySql filters, polymeric grouping, join query

Filters

Comparison Operators

  • Equal to: = (note not ==!)
  • Does not mean:! = Or <>
  • Greater than:>
  • Greater than or equal:> =
  • Less than: <
  • Less than or equal: <=
  • IS NULL
  • IS NOT NULL

Logical Operators

  • And: and
  • 或:or
  • Non-: not

Other operations

  • Sort by: order by
    • Positive sequence: asc
    • Reverse: desc
    • 例:select * from students order by age desc ;
  • Limit: limit
    • M data display control: limit m
    • The number of rows from the beginning of the subscript m is the n data show: limit m, n
    • 例:select * from students limit 3,2 ;
  • Deduplication: distinct
    • 例:select distinct * from students;
  • Fuzzy query: like
    • Any one character: _
    • Any number of characters:%
    • 例:select * from students where name like '%abc_' ;
  • Range queries
    • Continuous range: BETWEEN a AND b is equivalent to a <= value <= b
    • Return interval: IN
    • 例:select * from students where id in(1,3,5,7);
  • Alias: as


Aggregated packet

Common aggregate functions

  • Statistics Number: COUNT (column)
  • Maximum: MAX (column)
  • Minimum: MIN (column)
  • Sum: SUM (column)
  • Average: AVG (column)
  • All values ​​listed fields: GROUP_CONCAT (column)

Grouped query: group by

  • In case of a packet, the packet field and a polymerization field can only occur
  • Other field has no meaning, I will complain!
  • Select fields from table group by field;
  • Select field, count (*) from table group by field;

Polymerization Screening: having

  • Plus having conditional expressions may be made to limit the output results
  • Select fields from table group by 1 Field 1, Field 2 Field HAVING 2> = 80;

Execution order

  • If a query contains an alias at the same time (as), aggregate functions, where, having, then their order of execution
    • First execution: where
    • Then execute: a function of polymerization and aliases
    • The last execution: having


Subqueries

The results of a query to stay for the next query (select nested select)

Claim:

  • 1) nested inner query
  • 2) must always appear in parentheses

Example:

  • Find the average age of students
    • select avg(age) from students;
  • Find out the data is greater than the average age of
    • select * from student where age > 19;
  • The average age of SQL statements obtained is used to find the average age is greater than the statement
    • select * from students where age > (select avg(age) from students);


Join query

En: inner join

  • The unconditional connection:
    • The unconditional connection, also known as cross-connect / connected Cartesian
    • Each turn every combination will first table and another table
    • select * from 表1 inner join 表2
  • Conditional inner join:
    • On the basis of internal links on unconditional, plus a clause on
    • When connected, screened those records meaningful to combine
    • select * from 表1 as a inner join 表2 as b on a.id = b.id

Outer connecting: {left | right} join

  • Left outer: (in the left table reference)
    • When connecting two tables do not match the join condition when
    • Data left in the left table, the data table to the right filling NULL
    • select * from 表1 as a left join 表2 as b on a.id = b.id
  • Right outer join: (reference table to the right)
    • To make connections when the two tables, does not match the join condition when
    • Data left in the right table, the data table to the left to fill NULL
    • select * from 表1 as a right join 表2 as b on a.id = b.id




Guess you like

Origin www.cnblogs.com/jiyu-hlzy/p/11876717.html