Database | data sorting and grouping

A sort ORDER BY

① effect: the read data to sort
② general syntax:

SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1 [ASC [DESC][默认 ASC]], [field2...] [ASC [DESC][默认 ASC]]

Note:

  • You can add WHERE ... LIKE clause to set conditions
  • Use ASC or DESC keyword to set the query results in ascending or descending order. By default, it is ascending.

③ Examples: obtaining title data table htmlid

  • Default ascending order:
    Here Insert Picture Description
  • In descending order:
    Here Insert Picture Description
  • WHERE ... LIKE statement with a
    Here Insert Picture Description
    note: first with WHERE ... LIKE statement to filter information, and then ORDER BY ordering process information.

Second, the GROUP BY grouping

① universal grammar

SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Examples of the direct ②: a data packet field in accordance with the grade

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
(Note: Query largest user_id each group and the current group of content that max is a function, will not be discussed here first, its role is to find out. Brackets columns maximum , so understanding first on the line)

③ unconventional usage
Here Insert Picture Description
when the query more than one id, packet data in accordance with the grade, grade one are the same, max (user_id) is calculated only according to the data, how is the value of an id? id field is selected in accordance with the first retrieval data matches a stored mysql.
Conclusions: When the function group by polymerization, the polymerization while the non-use field, the value field is non-polymeric first matching field contents, the field contents i.e. small entry corresponding to id.
④WITH ROLLUP
role: on the basis of further statistics on the packet field group.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/Geekst/article/details/90299598