Chapters:
Data query and filtering 1.
2. Data of polymerization
between 3 tables connected
by data 4, change, deletelearning target:
Master the common SQL statement, skilled call data in the database to solve the problem
The core SQL statements classified by function:
- Data retrieval: SELECT FROM
- Filter data: WHERE / HAVING
- Data packet: GROUP BY
- Summary function: aggregation function
- Data Sort: ORDER BY
- Data Select: LIMIT
SQL statements constitute the core and the order of execution
SELECT [DISTINCT]
[列名],[聚合函数 AS <列名>]
FROM <表名>
[WHERE [<过滤条件>]
[GROUP BY <聚合类别列名>]
[HAVING <过滤条件>]
[ORDER BY<列名>(ASC|DESC) # 按照某一字段,升序或者降序排列]
[LIMIT <行数>]
# 执行顺序:
FROM > WHERE > GROUP BY > HAVING > SELECT(聚合函数) > ORDER BY > LIMIT
4 basic core SQL statements
Data query and filtering
data query:
- The basic query filter statements: the SELECT <column name> FROM <table name>
Data filtering
Filter statement based on a condition: the WHERE <Column name> value operator [AND / OR / NOT] column operator value
Query results handling
- Sorting Results: OEDER BY <column name> [ASC | DESC]
- Select specific results: LIMIT often used in conjunction with ORDER BY
- Query results deduplication: SELECT DISTINCT <column name> FROM <table name>
Data Aggregation
Syntax data aggregation
- SELECT <polymerization category> <aggregate functions ()> the FROM <table name>
- GROUP BY <polymerizable category>
Common aggregate functions:
- COUNT (l column names): Returns the number of rows in the specified column (not including NULL)
- COUNT (l *): Returns the number of rows in the specified column (including NULL)
- SUM (column names): Returns the value of the column total number (not including NULL)
- AVG (column names): Returns the value of the column average value (not including NULL)
- MIN / MAX (column names): returns the minimum / maximum value of the specified column (not including NULL)
Filter polymerization results:
- Use HAVING polymerization results and the filters filter
Sorting and selection polymerization results:
- Use ORDER BY and LIMIT
Inter-connect
- Equijoins: INNER JOIN - Returns the table on the left and the right key in the corresponding data table worth Airlines
- Left connector: LEFT JOIN - Returns the left and the right tables all rows of data corresponding to key value data table row
- Right connection: RIGHT JOIN - Returns the right and left rows form All the data table the row corresponding to the key value data
# 表间基本语法
SELECT<列名> FROM <左表名>
[INNER/ LEFT /RIGHT] JOIN <右表名>
ON <左表键值列名> = <右表键值列名>
Additions to delete data
Data table lists information:
# 列出数据表的信息:
DESCRIBE 表名
_________________
顾客号码 int(11) NO
姓 varchar(50) NO
名 varchar(50) NO
城市 varchar(125) YES
省份 varchar(125) YES
电话 varchar(125) YES
# 结果显示:1.数据表的字段(列);2.各个字段的数据类型
Increased data line:
how to add new records to the table?
- Field and the list will record the number, the newly recorded data have the same type
- The new master key record (eg: customer number) value can not be the same as any of the primary key table present in
# 增加数据行
INSERT INTO 表名(字段名)VALUES(新记录的字段值
————————————————————————————————————————————————————————
# 示例
INSERT INTO `顾客信息`(`顾客号码`,`姓`,`名`,`城市`,`省份`,`电话`)VALUES
(92,'王','光明','长春市','吉林省',13812455432)
# 查询插入的数据表记录是否插入到数据表中
SELECT *
FROM `顾客信息`
WHERE `顾客号码` = 92
Modify the field value specifies the data row:
# 修改指定数据行的字段值
UPDATE 表名 SET 字段名 = 字段值[ WHERE 过滤条件]
————————————————————————————————————————————————————————————
# 第一步:找到需要修改的记录(行),目的避免误操作修改不该修改的记录
SELECT * FROM `顾客信息` WHERE `顾客号码` = 92
# 第二步:对满足条件的表记录进行修改
## 使用UPDATE SET 和 WHERE 子句实现
## 修改语句可用于修改一条/多条记录
## 新值的数据类型必须与表中定义好的数据类型保持一致;
————————————————————————————————————————————————————————————
# 修改指定数据行的字段值
UPDATE `顾客信息`
SET `电话` = 13700000000
WHERE `顾客号码` = 92
# 使用SELECT 和 WHERE 语句确定修改完成
SELECT *
FROM `顾客信息`
WHERE `顾客号码` = 92
Remove the specified data lines:
DELETE FROM 表名 [WHERE 过滤条件 ]
————————————————————————————————
# 删除指定数据行#
## 使用DELETE FROM 和 WHERE子句实现
## 删除语句可用于一条/多条记录;
## 删除后使用过滤语句获得的结果为空
————————————————————————————————
## 确认要删除的是目标记录,避免误操作
SELECT *
FROM `顾客信息`
WHERE `顾客号码` = 92
DELETE FROM `顾客信息`
WHERE `顾客号码` = 92
## 可以使用SELECT和 WHERE语句确定删除完成
SELECT *
FROM `顾客信息`
WHERE `顾客号码` = 92
[Note]: modify and delete statements must be included with WHERE filter criteria to ensure data security