若是你對NodeJs系列感興趣,歡迎關注微信公衆號:前端神盾局或 github NodeJs系列文章本文整理自MySQL Tutorial和SQL必知必會html
MySQL CREATE TABLE Statement By Examples
CREATE TABLE [IF NOT EXISTS] table_name( column_list ) ENGINE=storage_engine
IF NOT EXISTS
是可選的,但推薦使用,它會先檢查是否有有同名表,若是沒有則建立。storage_engine
MySql 支持多種存儲引擎: MyISAM
、InnoDB
、MERGE
、MEMORY (HEAP)
、ARCHIVE
、CSV
、FEDERATED
,若是沒有指定,默認值是InnoDB
在建立表的時候須要給定字段(或列名),格式以下:前端
column_name data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
若是須要使用主鍵,可使用如下語法:mysql
PRIMARY KEY (col1,col2,...)
CREATE TABLE IF NOT EXISTS tasks ( task_id INT AUTO_INCREMENT, title VARCHAR(255) NOT NULL, start_date DATE, due_date DATE, status TINYINT NOT NULL, priority TINYINT NOT NULL, description TEXT, PRIMARY KEY (task_id) ) ENGINE=INNODB;
SELECT [alias1.]column_1, [alias1.]column_2, ... FROM table_1 [AS alias1],table_2 [AS alias2] [INNER | LEFT |RIGHT] JOIN table_3 ON conditions WHERE conditions GROUP BY column_1 HAVING group_conditions ORDER BY column_1 LIMIT offset, length;
MySQL WHERE
WHERE子句除了用在SELECT
中還能夠在UPDATE
、DELETE
中使用git
Operator | Description |
---|---|
= | 等於 |
<> or != | 不等於 |
< | 小於 |
> | 大於 |
<= | 小於等於 |
= | 大於等於
MySQL AND Operator
WHERE boolean_expression_1 AND boolean_expression_2
AND | TRUE | FALSE | NULL |
---|---|---|---|
TRUE | TRUE | FALSE | NULL |
FALSE | FALSE | FALSE | FALSE |
NULL | NULL | FALSE | NULL |
MySQL OR Operator
boolean_expression_1 OR boolean_expression_2
OR | TRUE | FALSE | NULL |
---|---|---|---|
TRUE | TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE | NULL |
NULL | TRUE | NULL | NULL |
須要注意的是AND操做符的優先級大於OR操做符github
MySQL BETWEEN
表示的是區間條件sql
expr [NOT] BETWEEN begin_expr AND end_expr;
expr
在/不在區間begin_expr
和end_expr
之間express
expr
、begin_expr
和end_expr
的數據類型必須相同微信
SELECT productCode, productName, buyPrice FROM products WHERE buyPrice BETWEEN 90 AND 100;
MySQL LIKE
expr LIKE 'pattern'
LIKE 支持如下通配符函數
MySQL IN
SELECT column1,column2,... FROM table_name1 WHERE (expr|column_1) [NOT] IN ('value1','value2',...);
若是column_1
或表達式的結果在集合中有匹配,將返回1不然返回0this
SELECT officeCode, city, phone, country FROM offices WHERE country IN ('USA' , 'France');
A Comprehensive Look at MySQL IS NULL Operator
value IS [NOT] NULL
注意:NULL表示無值,它與0、空字符、false不一樣
[]( http://www.mysqltutorial.org/...
根據一個或多個列對結果集進行分組,一般而言,GROUP BY
會和SUM
、AVG
、MAX
、MIN
等函數使用
SELECT c1, c2,..., cn, aggregate_function(ci) FROM table WHERE where_conditions GROUP BY c1 , c2,...,cn;
使用GROUP BY
須要注意如下幾點:
GROUP BY
子句能夠包含任意數目的列,於是能夠對分組進行嵌套,更細緻地進行數據分組。GROUP BY
必須在FROM
和WHREE
以後,ORDER BY
以前SELECT
中出現的非聚合函數字段,GROUP BY
語句中也應該存在,好比SELECT name, address, MAX(age) FROM t GROUP BY name;
運行此sql會報錯:
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mydb.t.address' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
爲了修復這個錯誤,咱們應該把address
字段從SELECT
中去掉或者在GROUP BY
中添加address
SELECT name, address, MAX(age) FROM t GROUP BY name,address;
不過有一些特例,具體能夠參考MySQL Handling of GROUP BY
MySQL HAVING
與GROUP BY
配合使用,用於過濾分組
HAVING condition
HAVING
支持全部WHERE
操做符(AND
、OR
、IN
、BETWEEN
、LIKE
)
MySQL ORDER BY: Sort a Result Set
SELECT column1, column2,... FROM tbl ORDER BY column1 [ASC|DESC], column2 [ASC|DESC],...
注意:
column1
排序再按column2
排序,這裏只有當column1
中的值相同時才按照column2
排序ASC
或DESC
,默認ASC
MySQL LIMIT
SELECT column1,column2,... FROM table LIMIT offset , count;
LIMIT
有兩個參數:
offset
: 指定開始的位置,從0開始count
: 表明要返回的行數
MySQL Subquery
使用子查詢的幾種形式:
WHERE
中使用SELECT lastName, firstName FROM employees WHERE officeCode IN (SELECT officeCode FROM offices WHERE country = 'USA');
SELECT customerNumber, checkNumber, amount FROM payments WHERE amount = (SELECT MAX(amount) FROM payments);
FROM
中使用SELECT MAX(items), MIN(items), FLOOR(AVG(items)) FROM (SELECT orderNumber, COUNT(orderNumber) AS items FROM orderdetails GROUP BY orderNumber) AS lineitems;
MySQL Join Made Easy For Beginners
在實際業務中,咱們常常把數據分表存放,那如何把多個表裏的數據用一個SELECT
語句查詢出來呢?這就須要聯結。聯結分紅如下幾種類型:Cross join、Inner join、Left join和Right join
下面經過一個例子來說解它們之間的區別。
首先咱們建立兩個表t1
和t2
CREATE TABLE t1 ( id INT PRIMARY KEY, pattern VARCHAR(50) NOT NULL ); CREATE TABLE t2 ( id VARCHAR(50) PRIMARY KEY, pattern VARCHAR(50) NOT NULL );
t1
和t2
表都有pattern
列,如今咱們插入一些數據
INSERT INTO t1(id, pattern) VALUES(1,'Divot'), (2,'Brick'), (3,'Grid'); INSERT INTO t2(id, pattern) VALUES('A','Brick'), ('B','Grid'), ('C','Diamond');
咱們先來看一下Cross join的用法:
SELECT t1.id, t2.id FROM t1 CROSS JOIN t2;
運行結果以下:
Cross join採用笛卡爾積的規則,其實是將兩個表相乘,獲得一個組合表(3*3條數據)
SELECT t1.id, t2.id FROM t1 INNER JOIN t2 ON t1.pattern = t2.pattern;
Inner join 其實是對Cross join的條件過濾,它將不知足t1.pattern = t2.pattern
的紀錄過濾掉:
獲取左表全部記錄,即便右表沒有對應匹配的記錄
SELECT t1.id, t2.id FROM t1 LEFT JOIN t2 ON t1.pattern = t2.pattern ORDER BY t1.id;
與Left join相反,用於獲取右表全部記錄,即便左表沒有對應匹配的記錄。
SELECT t1.id, t2.id FROM t1 RIGHT JOIN t2 on t1.pattern = t2.pattern ORDER BY t2.id;
MySQL UNION
用於組合多個SELECT
查詢
SELECT column_list UNION [DISTINCT | ALL] SELECT column_list UNION [DISTINCT | ALL] SELECT column_list
注意:
// 插入單行數據 INSERT INTO table(c1,c2,...) VALUES (v11,v12,...); // 插入多行數據 INSERT INTO table(c1,c2,...) VALUES (v11,v12,...), (v21,v22,...), ... (vnn,vn2,...);
注意:
MySQL UPDATE
UPDATE [LOW_PRIORITY] [IGNORE] table_name SET column_name1 = expr1, column_name2 = expr2, ... [WHERE condition];
UPDATE支持兩種修飾符:
LOW_PRIORITY
:延遲更新操做直到當前表沒有讀取操做,不過只有部分存儲引擎支持該修飾符,好比:MyISAM, MERGE, MEMORYIGNORE
:容許Mysql在發生錯誤時繼續更新操做MySQL DELETE
DELETE FROM table_name WHERE condition;
WHERE
條件沒有指定,就會刪除該表全部紀錄