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注入是什么,需要学习的是文 ...
随机推荐
- gtk+学习笔记(七)
今天被一个文本框坑了,基本设置什么的都对,但是就是无法显示中文,按钮名称都可以显示中文,先介绍下文本框的基本函数吧. GtkWidget *gtk_text_view_new(void);新建一个文本 ...
- 20155309南皓芯 实验2 Windows口令破解
在网络界,攻击事件发生的频率越来越高,其中相当多的都是由于网站密码泄露的缘故,或是人为因素导致,或是口令遭到破解,所以从某种角度而言,密码的安全问题不仅仅是技术上的问题,更主要的是人的安全意识问题. ...
- html meta标签使用总结(转)
之前学习前端中,对meta标签的了解仅仅只是这一句. <meta charset="UTF-8"> 但是打开任意的网站,其head标签内都有一列的meta标签.比如我博 ...
- javascript 去除最后一个字符自定义的方法
//公共去除最后字符方法 function dtrim(str, s){ var reg = eval("/"+s+"$/gi"); str=str.repla ...
- uc 下载页面 记录备份
记录一下 <!doctype html> <html> <head> <meta charset="utf-8"> <titl ...
- matlab .fig转化成pdf 缺失
1. 在matlab figure里面,Edit -> Copy Figure 2. 粘贴到ppt中即可
- redis 客户端命令
Redis 通过监听一个 TCP 端口或者 Unix socket 的方式来接收来自客户端的连接 1 .CLIENT LIST 返回连接到 redis 服务的客户端列表 2 .CLIENT SETNA ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
- hash课堂测试补分博客
题目要求: 开放地址法: 概念: 所谓的开放定址法就是一旦发生了冲突,就去寻找下一个空的散列地址,只要散列表足够大,空的散列地址总能找到,并将记录存入. 它的公式为: 解题过程(在下图中): 拉链法: ...
- hdu 4859 最小割
链接:点我 未懂