1. 聚合函数
1.1
- 聚合函数 : 计算从列中取得的值,返回一个值
- AVG() - 平均数
- COUNT() - 计数
- COUNT(column_name):指定列的值的数目(NULL不计入)
- COUNT(*):表中的记录数
- COUNT(DISTINCT column_name):指定列的不同值的数目
- FIRST(column_name) - 指定列第一个记录的值(只有MS Access支持)
- SQL Server语法:
SELECT TOP 1 column_name FROM table_name;
- MySQL语法:
SELECT column_name FROM table_name LIMIT 1;
- Oracle语法:
SELECT column_name FROM table_name WHERE ROWNUM <=1;
- SQL Server语法:
- LAST(column_name) - 指定列最后一个记录的值(只有MS Access支持)
- SQL Server语法:
SELECT TOP 1 column_name FROM table_name ORDER BY id DESC;
(id为自增ID) - MySQL语法:
SELECT column_name FROM table_name ORDER BY id DESC LIMIT 1;
- Oracle语法:
SELECT column_name FROM table_name ORDER BY id DESC WHERE ROWNUM <=1;
- SQL Server语法:
- MAX() - 最大值
- MIN() - 最小值
- SUM() - 和
1.2
- GROUP BY语句结合聚合函数,根据一个或多个列对多个结果集进行分组。
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
- GROUP BY 多表连接
SELECT table_1.column_name,aggregate_function(table_2.column_name) AS column_new_name
FROM table_2 LEFT JOIN table_1
ON table_2.id = table_id
GROUP BY table_1.column_name;
- HAVING字句筛选分组后的各组数据(WHERE关键字无法与聚合函数一起使用)
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
2. 纯量函数
- 纯量函数 :基于输入值,返回一个值
- UCASE() - 大写
- SQL语法:
SELECT UCASE(column_name) FROM table_name;
- SQL Server语法:
SELECT UPPER(column_name) FROM table_name;
- SQL语法:
- LCASE() - 小写
- SQL语法:
SELECT LCASE(column_name) FROM table_name;
- SQL Server语法:
SELECT LOWER(column_name) FROM table_name;
- SQL语法:
- MID() - 从某个文本字段提取字符
- SELECT MID(column_name, start, length) FROM table_name。start 必需,规定开始位置,起始值是1;length可选,省略的话返回剩余文本。
- SubString(字段, 1, end) - 从某个文本字段提取字符
- LEN() - 长度
- SQL语法:
SELECT LEN(column_name) FROM table_name;
- MySQL语法:
SELECT LENGTH(column_name) FROM table_name;
- SQL语法:
- ROUND(column_name, decimals) - 把数值字段舍入为指定的小数位数。
- decimals规定要返回的小数位数,如果decimals为0,结果将没有小数部分。
- NOW() - 当前系统日期和时间
SELECT NOW() FROM table_name;
- FORMAT() - 显示格式化
SELECT FORMAT(column_name, format) FROM table_name;
format为规定格式,如 DATE_FORMAT(NOW(), ‘%Y-%m-%。
- UCASE() - 大写
3. SQL快速参考
AND / OR - 条件与或
SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
ALTER TABLE - 修改表
ALTER TABLE table_name
ADD column_name datatype
或
ALTER TABLE table_name
DROP COLUMN column_name
AS (alias) - 别名
SELECT column_name AS column_alias
FROM table_name
或
SELECT column_name
FROM table_name AS table_alias
BETWEEN - 之间
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
CREATE DATABASE - 创建数据库
CREATE DATABASE database_name
CREATE TABLE - 创建数据表
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)
CREATE INDEX - 创建索引
CREATE INDEX index_name
ON table_name (column_name)
或
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
CREATE VIEW - 创建视图
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
DELETE - 删除
DELETE FROM table_name
WHERE some_column=some_value
或
DELETE FROM table_name
(Note: Deletes the entire table!!)
DELETE * FROM table_name
(Note: Deletes the entire table!!)
DROP DATABASE - 删除数据库
DROP DATABASE database_name
DROP INDEX - 删除索引
DROP INDEX table_name.index_name (SQL Server)
DROP INDEX index_name ON table_name (MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE table_name
DROP INDEX index_name (MySQL)
DROP TABLE - 删除整个表
DROP TABLE table_name
GROUP BY - 分组
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING - 选组
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
IN - 符合哪些值
SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
INSERT INTO - 插入新记录
INSERT INTO table_name
VALUES (value1, value2, value3,....)
或
INSERT INTO table_name
(column1, column2, column3,...)
VALUES (value1, value2, value3,....)
INNER JOIN - 内连接
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LEFT JOIN - 左连接
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
RIGHT JOIN - 右连接
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
FULL JOIN - 全连接
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LIKE - 像某种格式
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
ORDER BY - 排序
SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
ORDER BY - 拼音排序
- 如果字符集采用的是 gbk(汉字编码字符集),直接在查询语句后边添加 ORDER BY;如果字符集采用的是 utf8(万国码),需要先对字段进行转码然后排序:
SELECT *
FROM table_name
ORDER BY CONVERT(column_name using gbk);
SELECT - 查询某(些)列
SELECT column_name(s)
FROM table_name
SELECT * - 查询所有列
SELECT *
FROM table_name
SELECT DISTINCT - 查询不重复的列
SELECT DISTINCT column_name(s)
FROM table_name
SELECT INTO - 查询并插入
SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_table_name
或
SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_table_name
SELECT TOP - 查询前面的列值
SELECT TOP number|percent column_name(s)
FROM table_name
TRUNCATE TABLE - 删除表内容,保留表结构
TRUNCATE TABLE table_name
UNION - 联合
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
UNION ALL - 允许重复的值
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UPDATE - 更改表
UPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value
WHERE - 条件
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
4. 总结
SQL是用来访问和处理数据库系统的标准计算机语言,我们可以使用它在数据库中执行查询、获取数据、插入记录、删除记录、更新记录,还可以创建库、表、索引以及撤销它们。我们还应该掌握SQL中最重要的Aggregate函数。SQL是一种与数据库系统协同工作的标准语言,这些数据库包括MS SQL Server、IBM DB2、Oracle、MySQL和MS Access。