SQL:查询学习笔记
SQL 查询命令
SELECT
语法
SELECT "column_name"
FROM "table_name";
返回一列
SELECT Username FROM Users
返回多列
SELECT Username, Password From Users
返回所有列
SELECT * FROM Users
DISTINCT
语法
SELECT DISTINCT "column_name"
FROM "table_name";
示例
SELECT DISTINCT Name FROM Users
WHERE
语法
SELECT "column_name"
FROM "table_name"
WHERE "condition";
示例
SELECT * FROM Users WHERE Username = ‘HappyFramework’
AND OR
语法
SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{ [AND|OR] "simple condition"}+;
AND 示例
SELECT * FROM Orders Where Sales >= 1000 AND Sales <= 5000
OR 示例
SELECT * FROM Orders Where Sales < 1000 OR Sales > 5000
IN
语法
SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...);
示例
SELECT * FROM Users WHERE Username IN (Happyframework, shijiucha)
BETWEEN
语法
SELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2';
示例
SELECT * FROM Orders WHERE Sales BETWEEN 1000 AND 5000
LIKE
语法
SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN};
% 示例
SELECT * FROM Users WHERE Name LIKE '%光'
_ 示例
SELECT * FROM Users WHERE Name LIKE '段光_'
ORDER BY
语法
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC] {, "column_name" [ASC, DESC]}+;
示例
SELECT * FROM Users ORDER BY Username ASC
带公式的示例
SELECT * FROM Orders ORDER BY Receipt - Refund
AVG
语法
SELECT AVG("column_name")
FROM "table_name";
示例
SELECT AVG(Sales) FROM Sales
COUNT
语法
SELECT COUNT("column_name")
FROM "table_name";
示例
SELECT COUNT(DISTINCT *) FROM Sales
MAX
语法
SELECT MAX ("column_name")
FROM "table_name";
示例
SELECT MAX(Sales) FROM Sales
MIN
语法
SELECT MIN ("column_name")
FROM "table_name";
示例
SELECT MIN(Sales) FROM Sales
SUM
语法
SELECT SUM ("column_name")
FROM "table_name";
示例
SELECT SUM(Sales) FROM Sales
GROUP BY
语法
SELECT "column_name1", "column_name2", ... "column_nameN", Function("column_nameN+1")
FROM "table_name"
GROUP BY "column_name1", "column_name2", ... "column_nameN";
示例
SELECT Customer, SUM(Sales) FROM Orders GROUP BY Customer
Having
语法
SELECT ["column_name1"], Function("column_name2")
FROM "table_name"
[GROUP BY "column_name1"]
HAVING (arithmetic function condition);
示例
SELECT Customer, SUM(Sales)
FROM Orders
GROUP BY Customer
HAVING SUM(Sales) > 10000
ALIAS
语法
SELECT "table_alias"."column_name1" "column_alias"
FROM "table_name" "table_alias";
示例
SELECT U.Username 用户名 FROM Users U
AS
语法
SELECT "table_alias"."column_name1" "column_alias"
FROM "table_name" "table_alias";
示例
SELECT U.Username AS 用户名 FROM Users AS U
INNER JOIN
语法
SELECT "column_name"
FROM "left_table_name"
INNER JOIN "right_table_name" ON "left_table_name"."left_column" = "right_table_name"."right_column";
示例
SELECT Categories.Name, News.Content
FROM News
INNER JOIN Categories ON News.CategoryId = Categories.Id
OUTER JOIN
语法
SELECT "column_name"
FROM "left_table_name"
OUTER JOIN "right_table_name" ON "left_table_name"."left_column" = "right_table_name"."right_column";
示例
SELECT Categories.Name, News.Content
FROM News
OUTER JOIN Categories ON News.CategoryId = Categories.Id
LEFT OUTER JOIN
语法
SELECT "column_name"
FROM "left_table_name"
LEFT OUTER JOIN "right_table_name" ON "left_table_name"."left_column" = "right_table_name"."right_column";
示例
SELECT Categories.Name, News.Content
FROM News
LEFT OUTER JOIN Categories ON News.CategoryId = Categories.Id
RIGHT OUTER JOIN
语法
SELECT "column_name"
FROM "left_table_name"
RIGHT OUTER JOIN "right_table_name" ON "left_table_name"."left_column" = "right_table_name"."right_column";
示例
SELECT Categories.Name, News.Content
FROM News
RIGHT OUTER JOIN Categories ON News.CategoryId = Categories.Id
CROSS JOIN
语法
SELECT "column_name"
FROM "left_table_name", "right_table_name";
示例
SELECT Categories.Name, News.Content
FROM News, Categories
UNION
语法
[SQL Statement 1]
UNION
[SQL Statement 2];
示例
SELECT Name FROM Users
UNION
SELECT * FROM Employees
UNION ALL
语法
[SQL Statement 1]
UNION ALL
[SQL Statement 2];
示例
SELECT Name FROM Users
UNION ALL
SELECT * FROM Employees
INLINE VIEW
语法
SELECT "column_name" FROM (Inline View);
示例
SELECT * FROM
(SELECT * FROM Users)
INTERSECT
语法
[SQL Statement 1]
INTERSECT
[SQL Statement 2];
示例
SELECT Txn_Date FROM Store_Information
INTERSECT
SELECT Txn_Date FROM Internet_Sales;
MINUS
语法
[SQL Statement 1]
INTERSECT
[SQL Statement 2];
示例
SELECT Txn_Date FROM Store_Information
MINUS
SELECT Txn_Date FROM Internet_Sales;
TOP
语法
SELECT TOP N [PERCENT] "column_name"
FROM "table_name";
示例
SELECT TOP 2 Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
百分比示例
SELECT TOP 25 PERCENT Store_Name, Sales, Txn_Date
FROM Store_Information
ORDER BY Sales DESC;
SUBQUERY
语法
SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE "condition");
示例
SELECT SUM (Sales) FROM Store_Information
WHERE Store_Name IN
(SELECT Store_Name FROM Geography
WHERE Region_Name = 'West');
SELECT SUM (a1.Sales) FROM Store_Information a1
WHERE a1.Store_Name IN
(SELECT Store_Name FROM Geography a2
WHERE a2.Store_Name = a1.Store_Name);
EXISTS
语法
SELECT "column_name1"
FROM "table_name1"
WHERE EXISTS
(SELECT *
FROM "table_name2"
WHERE "condition");
示例
SELECT *
FROM Users
WHERE EXISTS (SELECT * FROM UserRoleRelations WHERE Users.Id = UserRoleRelations.UserId)
SIMPLE CASE
语法
SELECT CASE ("column_name")
WHEN "value1" THEN "result1"
WHEN "value2" THEN "result2"
...
[ELSE "resultN"]
END
FROM "table_name";
示例
SELECT Username, CASE (UserType)
WHEN 1 THEN "管理员"
WHEN 2 THEN "员工"
ELSE "未知"
FROM Users
SEARCHED CASE
语法
SELECT CASE
WHEN "condition1" THEN "result1"
WHEN "condition2" THEN "result2"
...
[ELSE "resultN"]
END
FROM "table_name";
示例
SELECT Username, CASE
WHEN UserType = 1 THEN "管理员"
WHEN UserType = 2 THEN "员工"
ELSE "未知"
FROM Users
SQL:查询学习笔记的更多相关文章
- 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句
原文:[SQL Server学习笔记]Delete 语句.Output 子句.Merge语句 DELETE语句 --建表 select * into distribution from sys.obj ...
- SQL查询(笔记2——实体查询)
SQL查询(笔记2——实体查询) 二.实体查询 如果查询返回了某个数据表的全部数据列,且该数据表有对应的持久化类映射,我们就把查询结果转换成实体查询.将查询结果转换成实体,可以使用SQLQuery提供 ...
- SQL server2005学习笔记(一)数据库的基本知识、基本操作(分离、脱机、收缩、备份、还原、附加)和基本语法
在软件测试中,数据库是必备知识,假期闲里偷忙,整理了一点学习笔记,共同探讨. 阅读目录 基本知识 数据库发展史 数据库名词 SQL组成 基本操作 登录数据库操作 数据库远程连接操作 数据库分离操作 数 ...
- sql查询学习和实践点滴积累
https://blog.rjmetrics.com/2008/10/28/correlated-subqueries-in-mysql/ http://www.mysqltutorial.org/m ...
- MVC中使用Entity Framework 基于方法的查询学习笔记 (二)
解释,不解释: 紧接上文,我们在Visual Studio2012中看到系统为我们自动创建的视图(View)文件Index.cshtml中,开头有如下这句话: @model IEnumerable&l ...
- 多线程处理慢sql查询小笔记~
多线程处理慢sql查询以及List(Array)的拆分 系统数据量不大,但是访问速度特别慢,使用多线程优化一下!!! 优化结果:访问时间缩短了十几秒 25s --> 8s 一.List的拆分: ...
- Java SQL注入学习笔记
1 简介 文章主要内容包括: Java 持久层技术/框架简单介绍 不同场景/框架下易导致 SQL 注入的写法 如何避免和修复 SQL 注入 2 JDBC 介绍 JDBC: 全称 Java Databa ...
- SQL之联合查询学习笔记
定义: 联合查询可合并多个相似的选择查询的结果集.等同于将一个表追加到另一个表,从而实现将两个表的查询组合到一起,使用谓词为UNION或UNION ALL. 语法格式 UNION 可以将两个或两个以上 ...
- sql注入学习笔记,什么是sql注入,如何预防sql注入,如何寻找sql注入漏洞,如何注入sql攻击 (原)
(整篇文章废话很多,但其实是为了新手能更好的了解这个sql注入是什么,需要学习的是文章最后关于如何预防sql注入) (整篇文章废话很多,但其实是为了新手能更好的了解这个sql注入是什么,需要学习的是文 ...
随机推荐
- (最大矩阵链乘)Matrix-chain product
Matrix-chain product. The following are some instances. a) <3, 5, 2, 1,10> b) < ...
- 【AtCoder】AGC011 C - Squared Graph
题解 大意是给出一张图,然后建一张新图,新图的点标号是(a,b) 如果a和c有一条边,b和d有一条边,那么(a,b)和(c,d)之间有一条边 我们把这道题当成这道题来做,给出两张图,如果第一张图有边( ...
- 易普优APS签约本田汽车零部件八千代工业集团!
2018年7月,易普优APS与八千代工业株式会社汽车零部件供应商正式签约,易普优APS在汽车零部件与整车行业的针对性解决方案的又一次得到客户高度认可与青睐! 日本八千代工业株式会社成立于1953年,总 ...
- 005 爬虫(requests与beautifulSoup库的使用)
一:知识点 1.安装requests库 2.Brautiful soup 可以提供一些简单的,python式的函数来处理导航,搜索,修改分析树等功能. 她是一个工具箱,通过解析文档为用户提供需要抓去的 ...
- 百度地图API--Key的获得
[开年后花了半个月的时间学习了百度地图API开发,准备投入项目中,学习的过程中写了一些简单的总结,在部门内部做了一个简单的分享培训,这里希望将自己的仅有的一点点关于百度地图API的收获分享给社区,整个 ...
- [python selenium] 操作方法整理
个人笔记,摘抄自虫师python selenum,仅供个人参考 1.安装: pip install selenium 下载webdriver # webdriver 下载并放置在python主目录 · ...
- 机器学习:KNN-近邻算法
一.理论知识 1.K近邻(k-Nearest Neighbor,简称KNN)学习是一种常用的监督学习. 工作机制:给定测试样本,基于某种距离度量找出训练集中与其最靠近的k个训练样本,然后基于这k个的信 ...
- windows上springboot打war部署tomcat小记
web项目,需要部署到云主机里去,现在windows里试一下. springboot项目,主要流程就只是打成war包后扔到tomcat里去,但是由于是springboot项目,有一些需要注意的地方,这 ...
- 错误: No API token found for service account "default",
[root@kubernetes-master pods]# kubectl create -f mysql.yaml Error from server (ServerTimeout): error ...
- leetcode 算法 Excel表列序号 python实现
这道题给我感觉就像一个26进制数一样. A 就是1 B是2 .... Z 是26 如果AB 两位,那就是 1 * 26 + 2 就是A 的数值*26 + B的数值 如果是MNP 三位数 那就 ...