MySQL (two) - DQL data query language

Brief 1. DQL

Data Query LANGUAGE: Data Query Language

  • All queries are using it SELECT
  • Simple queries, complex queries can do it ~
  • Database in the core language, the most important statement
  • The most frequently used statement

SELECT full syntax
Here Insert Picture Description
Note: [] represents optionally, {} represents Required

2. Specify the query field

(1) query all of the student
syntax: SELECT * FROM table
Here Insert Picture Description
(2) query specified field
syntax: SELECT fields FROM table
Here Insert Picture Description
(3) alias, to the result of a name
AS can play to field alias, can also play an alias to the table
syntax: SELECT field 1 AS alias 1, field 2 AS alias 2 FROM table name AS alias
Here Insert Picture Description
(4) function Concat (a, B)
Here Insert Picture Description
(. 5) to re distinct
functions: removing SELECT query out of the results of duplicate data, duplicate data show that only one

  • All queries test scores
    Here Insert Picture Description
  • Discover what the students took the exam
    Here Insert Picture Description
  • Found duplicate data, deduplication
    Here Insert Picture Description

(6) database columns (expression)

  • Query system version (Function)
    Here Insert Picture Description
  • Used to calculate the (expression)
    Here Insert Picture Description
  • Query increment step size (variable)
    Here Insert Picture Description
  • Student test scores + 1 points View

Here Insert Picture Description
Note that
the database expressions: text value column, Null, function calculation expression system variables ....
SELECT expression from table

3. where conditional clause

Action: retrieve data values meet the conditions
of the condition search by one or more expressions! Boolean result
(1) logical operators
Here Insert Picture Description
try to use letters

  • Queries test scores between 95 to 100 points
    Here Insert Picture Description
  • Fuzzy query (section)
    Here Insert Picture Description
  • In addition to students outside the student achievement No. 1000
    Here Insert Picture Description

(2) fuzzy query: Comparison operators
Here Insert Picture Description

  • Query students surnamed Liu, combined with like% (on behalf of 0 to any number of characters)
    Here Insert Picture Description

  • Query students surnamed Liu, _ back (a character) name is only one word
    Here Insert Picture Description

  • Query students surnamed Liu, __ behind (a character) name only two words
    Here Insert Picture Description

  • Middle name query word Ka Ka% of students%
    Here Insert Picture Description

  • Query No. 1001, students
    Here Insert Picture Description

  • Queries students in Anhui
    Here Insert Picture Description

  • Queries address is empty student null ''
    Here Insert Picture Description

  • Queries date of birth of students is not empty
    Here Insert Picture Description

4. Union-table query

  • JOIN contrast
    Here Insert Picture Description
    Here Insert Picture Description

  • Between the three types
    Here Insert Picture Description

  • Exercise 1

Query took the exam students: Student ID, name, account number, fractional
thinking ① demand analysis, which analyzes the query from the table field, (join queries)
② determine which connection to use the query? 7 kinds
③ determining intersection points (the two data tables which are the same)
④ determined by: the student studentNo = Table transcript studentNo
Here Insert Picture Description

  • English II

Inquired the students take the exam information: school, student name, account name, scores
ideas ① demand analysis, field analysis from which the query tables, student, result, subject (join queries)
② determine which connection to use the query? 7 kinds
③ determining intersection points (the two data tables which are the same)
④ determined by: the student studentNo = Table transcript studentNo
Here Insert Picture Description

Summary: I have to find ... what data the SELECT
the FROM table from which to search several tables
Table XXX Join connected
on cross-conditionality
assume the existence of a multiple-table query, slowly, to query two tables and then slowly increase

The paging and sorting

(1) Sort

  • ASC ASC, descending DESC
  • ORDER BY sorting through which field, how row
  • Exercise: results of the query results sorted in descending order
    Here Insert Picture Description

(2) Paging
Paging reasons: to ease the pressure database, gives the experience better, waterfall
syntax: limit the starting value, page size
N-page limit (the n--1) * pageSize, pageSize
(the n--1) * pageSize : start value
the pageSize: page size
n: this page
total data / page = total number of pages
Here Insert Picture Description

6. subquery

Essence: nested in the where clause in a subquery

  • Exercise 1: query the database structure of all test results-1 (student number, account number, grades), in descending order
    Method 1: Use join queries
    Here Insert Picture Description
    way: using sub-queries (from the inside and outside)
    Here Insert Picture Description

  • Exercise Two: Queries courses for advanced mathematics -2 and no score is not less than 80 students of the school number and name of the
    Way: Use join queries
    Here Insert Picture Description
    way: using sub-queries (from the inside and outside)
    Here Insert Picture Description

7. packet filtering and

Core: grouped according to different courses
the GROUP BY ...
the HAVING ...

Exercise: Query different courses average, highest score, lowest score, on average more than 80 points
Here Insert Picture Description

8. Select Summary

Here Insert Picture Description

Published 62 original articles · won praise 2 · Views 2738

Guess you like

Origin blog.csdn.net/nzzynl95_/article/details/104085327
Recommended