Common learning basic statement mysql

Common sql statement

Inquire:

SELECT column names (or * indicates all columns) FROM table WHERE filter criteria;

FROM table name: the name suggests, is specified in the table name from this table;
the WHERE filter criteria: means "to meet the filter criteria when" time;
the SELECT column names: means choosing these records, and demonstrate the column name specified;

Out of the student's name table tb_stu_math_score score is greater than 95 minutes;
the SELECT name the FROM tb_stu_math_score the WHERE Score> = 95;

Selected from the table tb_stu_math_score score less than 60 points of the student's name,
the SELECT name, the FROM tb_stu_math_score the WHERE Score Number <60;

Tb_stu_math_score pick from the score table 100 students of all information (SELECT behind the note symbol, indicates all fields),
SELECT
the FROM tb_stu_math_score the WHERE Score = 100;

Advanced Usage:
If table tb_rider, tb_order two tables

IN operator
records WHERE id IN (2, 4, 7) means that the value of the id field screening 2,4,7 among these values,
the SELECT name, Level, level_city the FROM tb_rider the WHERE id IN (2,. 4, 7);

Relationship operators AND and OR

Queries tb_rider`` table creation time in data between 2017-02-01 to 2017-06-01.
SELECT * FROM tb_rider WHERE created_at> = "2017-02-01 00:00:00" AND created_at < "2017-06-01 00:00:00";

Query tb_rider``2017-02-01 (including that day) before, or 2017-06-01 (including the day) after the registration of riders all the information.
SELECT * FROM tb_rider WHERE created_at <= "2017-02-01 00:00:00" OR created_at> = "2017-06-01 00:00:00";

Query tb_rider``2017-02-01 (including 2017-02-01 day) to 2017-06-01 (not including the day 2017-06-01) registered during the gold medal and the current level is the rider all the information,
the SELECT * the FROM tb_rider
the WHERE the created_at> = "2017-02-01 00:00:00"
the AND the created_at < "2017-06-01 00:00:00"
the AND Level =. 3;

Sort: ORDER BY DESC: descending ASC: Increment

According to the creation time of consignment reverse order to AWB recently created at the top, use the ORDER BY syntax: created_at field values descending (DESC) order to sort query results
the SELECT order_id, created_at the FROM tb_order
the WHERE rider_id = 1
the AND grabbed_time> = " 00:00:00 2017-12-30 "
the AND grabbed_time <" 2017-12-31 00:00:00 "
the AND 40 = order_state
the ORDER BY the created_at DESC;

More complex scenario: Suppose you want to query all the information 2017-12-30 and 2017-12-31 two days all the waybill, and in accordance with the first rider id increment, then the status of the consignment descending order sorting show.
* The FROM tb_order the WHERE the created_at the SELECT> = "2017-12-30 00:00:00" the created_at the AND < "2018-01-01 00:00:00"
the ORDER BY rider_id the ASC, order_state DESC;
ordering priority is: if value rider_id indistinguishable fields have the same sequence, only a few of which the same data is then sorted rider_id value order_state fields: for example, rider_id = 2 and data order_state = 80, the row is still impossible rider_id = foregoing data 1 and the order_state = 40.

Paging

20 lines before the data query
SELECT created_at FROM tb_order WHERE rider_id = 1 LIMIT 20;

Aggregation functions: COUNT, SUM, AVG

COUNT: In a particular column counts the query result set;
the SUM: a field for summing query results;
the AVG: is the average mean, calculating an average value of a field in a query result;

#### COUNT seeking total
COUNT brought the total number of general statistics: COUNT (name): represents the name of this column statistics such as (Note that COUNT is not calculated for the line name = null: for example, there are 20 lines total but there is a line name = null, then the result is actually figured out 19):
the SELECT COUNT ( ) the FROM tb_order; (tables total tb_order: simple number of statistics)
the SELECT COUNT (
) the FROM tb_order the wHERE rider_id = 1; (statistics table tb_order conditions rider_id = 1 Total: Total satisfies condition) simple statistical
SELECT COUNT (name) FROM tb_order; ( number of statistics tb_order: name column subject to)
the SELECT COUNT (name) the FROM tb_order the WHERE rider_id = 1; (statistics tb_order conditions rider_id = 1 Total: column name to date)

Precautions: 1.COUNT not automatically deduplication;
2.COUNT in a certain query results to the row count value of "empty" when this record is not counted;

#### SUM 求和
SELECT SUM(merchant_customer_distance) FROM tb_order WHERE rider_id = 1 AND order_state = 40;

#### AVG 求和
SELECT AVG(merchant_customer_distance) FROM tb_order WHERE rider_id = 1 AND order_state = 40;

Deduplication: DISTINCT

COUNT (DISTINCT name) is the name of the de-duplicated counting
SELECT COUNT (DISTINCT name) FROM tb_sample_1 ;

Query data packet: GROUP BY Syntax

SELECT rider_id, rider_name, AVG(merchant_customer_distance)
FROM tb_order WHERE order_state = 40
AND created_at >= "2017-12-30 00:00:00"
AND created_at < "2017-12-31 00:00:00"
GROUP BY rider_id;

Aggregate function good partner: HAVING syntax

HAVING语法的含义类似于WHERE,之所以有HAVING函数是因为WHERE语法是不能和聚合函数一起使用的,因此只能使用HAVING作为筛选条件的语法

GROUP BY rider_id将SELECT的结果根据rider_id进行分组,分组完成后HAVING AVG(merchant_customer_distance) > 1.5语句对每一组的merchant_customer_distance字段值求取平均数,并且将平均数大于1.5的结果筛选出来,作为返回结果。

SELECT rider_id, rider_name, AVG(merchant_customer_distance)
FROM tb_order WHERE order_state = 40
AND created_at >= "2017-12-30 00:00:00"
AND created_at < "2017-12-31 00:00:00"
GROUP BY rider_id
HAVING AVG(merchant_customer_distance) > 1.5;

Sql data type often include several types of contact.

Integer:
. 1: tinyint: integer used to represent very small, it is often used such as is_deleted, is_valid field types of these fields, since the two field indicates whether the record is valid, only two values are and 0. 1;
2: smallint the: tinyint be slightly larger than the integer bit, can represent integers larger point, such as an integer value such 200,404,401;
. 3: int: integer used, may be used to represent large integer, such as 10322 (int may in fact represent an integer range is much larger than this);
4: bigint: used to represent a very large integer, such as auto-increment id most tables will use this type, 10,322,903 represents such a very large integer (in fact bigint integer range may represent far larger than this) is similar;

Float:
1: decimal: decimal can be expressed very accurate, such as latitude and longitude;

String type
char: fixed-length string;
VARCHAR: a variable length string;

Type Date
date: indicates a date, only the portion containing the date, the time is not included, such as the current date "2018-01-23";
datetime: represents a date, time and date portion contains portions, such as the current date "2018-01- 23 03:01:43 ";

Guess you like

Origin www.cnblogs.com/amysu/p/10951139.html