function
COUNT () / count, MIN () / minimum value, MAX () / maximum value, AVG () / mean value, SUM () / and
Clause
Clause is part of the statement include WHERE, GROUP, ORDER, LIMIT
-
WHERE: Conditions
-
GROUP: Merge (clustering, in fact, the same combined)
-
ORDER: Sort
-
LIMIT: limit (limit output)
WHERE
-
WHERE name='blue'
-
WHERE age>18
-
WHERE age<=18
-
WHERE age>=18 AND score<60
-
WHERE cach>100 OR score>10000
删 -DELETE
DELETE FROM table WHERE condition; delete one of the data specified in the table
DELETE FROM `user_table` WHERE ID = '$ {res.query.id}'; deleting data in accordance with a specified id specified all fields
Note: There is no way to delete only one field, only delete a row
改-UPDATE
SET UPDATE Table = Value field, field = value, the WHERE condition ...;
UPDATE `article_data` SET` n_like = n_like + 1` WHERE ID = xxx; update id specified according to the specified data field n_like
Charles -SELECT
SELECT * FROM table WHERE condition;
SELECT * FROM `user_table` WHERE ID = '$ {res.query.id}'; id specified data according to the specified query all fields
Note: WHERE is operating without an entire table, can be used in addition to INSERT WHERE
GROUP
-
GROUP BY class
- GROUP BY name
Charles -SELECT
SELECT * FROM table GROUP BY field;
SELECT * FROM student_table GROUP BY class; expressed in class as a basis for grouping, as long as the same class will certainly merge, leaving only a same class data, do a de-emphasis
SELECT class, COUNT (class) FROM student_table GROUP BY class; only query class field to class based grouping, and counting the same class
SELECT class, AVG (score) FROM student_table GROUP BY class; query to obtain an average value per class
SELECT class, MAX (score), MIN (score) FROM student_table GROUP BY class; each query to get the lowest score the highest class
SELECT name, SUM (price) FROM sales_table GROUP BY name; query to get total spending per person
Note: GROUP alone does not make sense that we should cooperate with function (COUNT / count, MIN / minimum value, MAX / maximum, AVG / mean value, SUM / and)
ORDER
-
ORDER BY age ASC
-
ORDER BY age DESC
- ORDER BY SUM(price)
ASC- ascending (small to large) / DESC- descending order (descending)
Charles -SELECT
SELECT * FROM table ORDER BY the ASC field; single sort conditions
SELECT * FROM table ORDER BY field ASC, field DESC; multiple sort conditions
SELECT * FROM student_table ORDER BY price DESC; data look-up table in descending order and subject to price
SELECT * FROM TABLE ORDER BY price ASC, sales DESC; data look-up table in ascending order and subject to price, sales price subject to the same descending order
SELECT name, SUM (price) FROM sales_table GROUP BY name ORDER BY SUM (price) DESC; query to get the total consumption of each person in descending order
SELECT name, SUM (price) FROM sales_table GROUP BY name ORDER BY SUM (price) ASC; query to get the total consumption of each person in ascending order
LIMIT
-
LIMIT 10; the first 10
-
LIMIT 5,8; 5 from the beginning to 8
Charles -SELECT
SELECT * FROM table LIMIT digital;
SELECT * FROM student_table LIMIT 2; first two data query table
Paging
Method a: all data for the front end, the front end of each page of data to the control (the amount of data do not fly)
Method two: just give a little bit of background data, the back-end data to control page (actually limits in the database)
Use Method Two If you want to do pagination, page 20 data:
-
Page 1 limit 0,20 0 ~ 19
-
Page 2 limit 20,20 20 ~ 39
-
Page 3 limit 40,20
-
N-th page limit (n-1) * 20,20
Order between clauses
There is an order between clauses: WHERE GROUP ORDER LIMIT
Note: no can not write
Example:
SELECT class,COUNT(class) FROM student_table
WHERE score>60
GROUP BY class
ORDER BY COUNT(class) DESC
LIMIT 2;