转自Github:enochtangg/quick-SQL-cheatsheet



目录

  1. 查找数据的查询
  2. 修改数据的查询
  3. 聚合查询
  4. 连接查询
  5. 视图查询
  6. 修改表的查询



1. 查找数据的查询

1.1 SELECT: 用于从数据库中选择数据

SELECT * FROM table_name;

1.2 DISTINCT: 用于过滤掉重复的值并返回指定列的行

 SELECT DISTINCT column_name;

1.3 WHERE: 用于过滤记录/行

 SELECT column1, column2 FROM table_name WHERE condition;

 SELECT * FROM table_name WHERE condition1 AND condition2;

 SELECT * FROM table_name WHERE condition1 OR condition2;

 SELECT * FROM table_name WHERE NOT condition;

 SELECT * FROM table_name WHERE condition1 AND (condition2 OR condition3);

 SELECT * FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);

1.4 ORDER BY: 用于结果集的排序,升序(ASC)或者降序(DESC)

 SELECT * FROM table_name ORDER BY column;

 SELECT * FROM table_name ORDER BY column DESC;

 SELECT * FROM table_name ORDER BY column1 ASC, column2 DESC;

1.5 SELECT TOP: 用于指定从表顶部返回的记录数

 SELECT TOP number columns_names FROM table_name WHERE condition;

 SELECT TOP percent columns_names FROM table_name WHERE condition;

 --并非所有数据库系统都支持SELECT TOP。 MySQL 中是LIMIT子句
SELECT column_names FROM table_name LIMIT offset, count;

1.6 LIKE: 用于搜索列中的特定模式,WHERE 子句中使用的运算符

% (percent sign) 是一个表示零个,一个或多个字符的通配符

_ (underscore) 是一个表示单个字符通配符

 SELECT column_names FROM table_nam
WHERE column_name LIKE pattern; pattern可以如下 LIKE ‘a%’ (查找任何以“a”开头的值) LIKE ‘%a’ (查找任何以“a”结尾的值) LIKE ‘%or%’ (查找任何包含“or”的值) LIKE ‘_r%’ (查找任何第二位是“r”的值) LIKE ‘a_%_%’ (查找任何以“a”开头且长度至少为3的值) LIKE ‘[a-c]%’(查找任何以“a”或“b”或“c”开头的值)

1.7 IN: 用于在 WHERE 子句中指定多个值的运算符

本质上,IN运算符是多个OR条件的简写

 SELECT column_names FROM table_name
WHERE column_name IN (value1, value2, …); SELECT column_names FROM table_name
WHERE column_name IN (SELECT STATEMENT);

1.8 BETWEEN: 用于过滤给定范围的值的运算符

 SELECT column_names FROM table_name
WHERE column_name BETWEEN value1 AND value2; SELECT * FROM Products
WHERE (column_name BETWEEN value1 AND value2) AND NOT column_name2 IN (value3, value4); SELECT * FROM Products WHERE column_name BETWEEN #01/07/1999# AND #03/12/1999#;

1.9 NULL: 代表一个字段没有值

 SELECT * FROM table_name
WHERE column_name IS NULL; SELECT * FROM table_name
WHERE column_name IS NOT NULL;

1.10 AS: 用于给表或者列分配别名

 SELECT column_name AS alias_name FROM table_name;

 SELECT column_name FROM table_name AS alias_name;

 SELECT column_name AS alias_name1, column_name2 AS alias_name2;

 SELECT column_name1, column_name2 + ‘, ‘ + column_name3 AS alias_name;

1.11 UNION: 用于组合两个或者多个 SELECT 语句的结果集的运算符

  • 每个 SELECT 语句必须拥有相同的列数
  • 列必须拥有相似的数据类型
  • 每个 SELECT 语句中的列也必须具有相同的顺序
 SELECT columns_names FROM table1
UNION SELECT column_name FROM table2;
  • UNION 仅允许选择不同的值, UNION ALL 允许重复

1.12 ANY|ALL: 用于检查 WHERE 或 HAVING 子句中使用的子查询条件的运算符

  • ANY 如果任何子查询值满足条件,则返回 true。
  • ALL 如果任何子查询值满足条件,则返回 true。
 SELECT columns_names FROM table1
WHERE column_name operator (ANY|ALL) (SELECT column_name FROM table_name WHERE condition);

1.13 GROUP BY: 通常与聚合函数(COUNT,MAX,MIN,SUM,AVG)一起使用,用于将结果集分组为一列或多列

 SELECT column_name1, COUNT(column_name2) FROM table_name
WHERE condition
GROUP BY column_name1
ORDER BY COUNT(column_name2) DESC;

1.14 HAVING: HAVING 子句指定 SELECT 语句应仅返回聚合值满足指定条件的行。它被添加到 SQL 语言中,因为WHERE关键字不能与聚合函数一起使用。

 SELECT COUNT(column_name1), column_name2 FROM table
GROUP BY column_name2
HAVING COUNT(column_name1) > 5;



2. 修改数据的查询

2.1 INSERT INTO: 用于在表中插入新记录/行

 INSERT INTO table_name (column1, column2) VALUES (value1, value2);

 INSERT INTO table_name VALUES (value1, value2 …);

2.2 UPDATE: 用于修改表中的现有记录/行

 UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition; UPDATE table_name
SET column_name = value;

2.3 DELETE: 用于删除表中的现有记录/行

 DELETE FROM table_name
WHERE condition; DELETE * FROM table_name;



3. 聚合查询

3.1 COUNT: 返回出现次数

 SELECT COUNT (DISTINCT column_name);

3.2 MIN() and MAX(): 返回所选列的最小/最大值

 SELECT MIN (column_names) FROM table_name
WHERE condition; SELECT MAX (column_names) FROM table_name
WHERE condition;

3.3 AVG(): 返回数字列的平均值

SELECT AVG (column_name) FROM table_name
WHERE condition;

3.4 SUM(): 返回数值列的总和

 SELECT SUM (column_name) FROM table_name
WHERE condition;



4. 连接查询

4.1 INNER JOIN: 内连接,返回在两张表中具有匹配值的记录

 SELECT column_names
FROM table1 INNER JOIN table2
ON table1.column_name=table2.column_name; SELECT table1.column_name1, table2.column_name2, table3.column_name3
FROM
(
(table1 INNER JOIN table2 ON relationship)
INNER JOIN
table3
ON relationship
);

4.2 LEFT (OUTER) JOIN: 左外连接,返回左表(table1)中的所有记录,以及右表中的匹配记录(table2)

 SELECT column_names
FROM table1 LEFT JOIN table2
ON table1.column_name=table2.column_name;

4.3 RIGHT (OUTER) JOIN: 右外连接,返回右表(table2)中的所有记录,以及左表(table1)中匹配的记录

 SELECT column_names
FROM table1 RIGHT JOIN table2
ON table1.column_name=table2.column_name;

4.4 FULL (OUTER) JOIN: 全外连接,全连接是左右外连接的并集. 连接表包含被连接的表的所有记录, 如果缺少匹配的记录, 以 NULL 填充。

 SELECT column_names
FROM table1 FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

4.5 Self JOIN: 自连接,表自身连接

 SELECT column_names
FROM table1 T1, table1 T2
WHERE condition;



5. 视图查询

5.1 CREATE: 创建视图

 CREATE VIEW view_name
AS
SELECT column1, column2
FROM table_name WHERE condition;

5.2 SELECT: 检索视图

 SELECT * FROM view_name;

5.3 DROP: 删除视图

 DROP VIEW view_name;



6. 修改表的查询

6.1 ADD: 添加字段

 ALTER TABLE table_name ADD column_name column_definition;

6.2 MODIFY: 修改字段数据类型

 ALTER TABLE table_name MODIFY column_name column_type;

6.3 DROP: 删除字段

 ALTER TABLE table_name DROP COLUMN column_name;



Review: Basic Knowledge about SQL的更多相关文章

  1. Review: Basic Knowledge about JavaScript 1

    JavaScript shanzm

  2. Review: Basic Knowledge about WebForm

    Asp.net shanzm

  3. NLP related basic knowledge with deep learning methods

    NLP related basic knowledge with deep learning methods  2017-06-22   First things first >>> ...

  4. Python基础知识(Basic knowledge)

    Python基础知识(Basic knowledge) 1.认识Python&基础环境搭建 2.Python基础(上) 3.Python基础(中) 4.Python基础(下) 5.Python ...

  5. Bat:Basic knowledge(同时运行多条命令,连接SqlServer执行sql,单个bat打开多个tomcat,cmd切换命令行编码,根据PID结束端口号)

    1.Windows7环境下命令行一次运行多条命令 Windows7命令行(cmd)下,如果想一次运行多条命令可能用到的连接符个人了解到的有三个:&&,||和&. aa & ...

  6. 计算机基础知识 一 Basic knowledge of computers One

    计算机硬件由CPU(Central Processing Unit).存储器.输入设备.输出设备组成. CPU通常由控制单元(控制器)和算数逻辑单元(运算器)组成. 运算器:负责进行算数运算和逻辑运算 ...

  7. Android Studio accelerator key(shortcut)& Basic knowledge

    shift + F6 重构(选文件,ok->下面的控制台,do refactor option + return 快速修复 Activity@Extra() Intent: @FragmentA ...

  8. NoSql basic knowledge

    The big picture to keep in mind first is: There are lots of articles and resources out there: http:/ ...

  9. [Tango] Basic Knowledge

    Project Tango类设备能够给开发者在哪些领域带来机会. 室内导航*:室内GPS信号的缺失,使得Project Tango设备会成为室内导航重要应用场景之一.有了它,你就不会在不熟悉的室内商场 ...

随机推荐

  1. [Swift]LeetCode904. 水果成篮 | Fruit Into Baskets

    In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...

  2. [Swift]LeetCode940. 不同的子序列 II | Distinct Subsequences II

    Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...

  3. Build Assimp library for Android

    Build Assimp library for Android 首先各路教程中有推荐使用 NDK 或者 STANDALONE TOOLCHAIN 编译的,根据我的理解,这两种方式都是可以的,如果能直 ...

  4. J2EE-tomcat的配置

    修改web.xml文件里面的内容:  路径:D:\software\apache-tomcat-8.0.44\webapps\ROOT\WEB-INF\web.xml: 内容:<?xml ver ...

  5. python获取当前路径

    python获取当前执行命令的路径: #!/usr/bin/env python # -*# coding: utf-8 -*- import os print os.getcwd() python获 ...

  6. JDK1.8源码(一)——java.util.ArrayList

      ArrayList 定义 ArrayList 是一个用数组实现的集合,支持随机访问,元素有序且可以重复. public class ArrayList<E> extends Abstr ...

  7. DataRead和DataSet的异同

    第一种解释 DataReader和DataSet最大的区别在于,DataReader使用时始终占用SqlConnection(俗称:非断开式连接),在线操作数据库时,任何对SqlConnection的 ...

  8. 基于spark实现并行化Apriori算法

    详细代码我已上传到github:click me 一. 实验要求         在 Spark2.3 平台上实现 Apriori 频繁项集挖掘的并行化算法.要求程序利用 Spark 进行并行计算. ...

  9. flag.xls

    Topic Link http://ctf5.shiyanbar.com/misc/flag.xls 1)  打开表格发现需要密码 但是flag又在表里,但我们可以改他的格式为.txt文本,因为这样就 ...

  10. 菜鸟系列docker——docker基本概念(1)

    docker基本概念 1.准备 这里先介绍容器技术,后续再介绍docker.docker是容器的一种,除docker以外,还存在coreos.不过在当前趋势下容器和docker基本上可以划为等号了. ...