注意:该博客SQL语句中的表均为博主本地数据库中的表[Smart_project]
1.选择Manager.[User]表中的ID,Email,DisplayName列:
SELECT ID,Email,DisplayName FROM Manager.[User]
2.选择Manager.[User]表中的所有列:
SELECT * FROM Manager.[User]
3.选择Manager.[User]表中的password列并去重:
SELECT DISTINCT password FROM Manager.[User]
4.选择Manager.[User]表中的password = 123456的数据:
SELECT password FROM Manager.[User] WHERE Password = '123456'
SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。
可在 WHERE 子句中使用的运算符:
=等于、<>不等于、>大于、<小于、>=大于等于、<=小于等于、BETWEEN在某个范围内、LIKE搜索某种模式
5.AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来:
SELECT * FROM Manager.[User]
WHERE Password = '123456' AND LoginName = 'Iverson.Tian'
SELECT * FROM Manager.[User]
WHERE Password = '1234' OR LoginName = 'Iverson.Tian'
6.ORDER BY 语句用于对结果集进行排序:
正序:
SELECT * FROM Manager.[User]
ORDER BY PostID
倒序<DESC>:
SELECT ID,PostID,Phone FROM Manager.[User]
ORDER BY PostID DESC
7.INSERT INTO 语句用于向表格中插入新的行:http://www.w3school.com.cn/sql/sql_insert.asp
8.Update 语句用于修改表中的数据:http://www.w3school.com.cn/sql/sql_update.asp
9.DELETE 语句用于删除表中的行:http://www.w3school.com.cn/sql/sql_delete.asp
10.TOP 子句用于规定要返回的记录的数目:http://www.w3school.com.cn/sql/sql_top.asp
前2条:
SELECT TOP 2 ID,DisplayName FROM Manager.[User]
前50%:
SELECT TOP 50 PERCENT ID,DisplayName FROM Manager.[User]
11.LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式:
Password以S开头:
SELECT * FROM Manager.[User] WHERE Password LIKE 'S%'
Password以S结尾:
SELECT * FROM Manager.[User] WHERE Password LIKE '%S'
Password中包含S:
SELECT * FROM Manager.[User] WHERE Password LIKE '%S%'
Password中不包含S:
SELECT * FROM Manager.[User] WHERE Password NOT LIKE '%S%'
12.SQL 通配符:http://www.w3school.com.cn/sql/sql_wildcards.asp
注意:SQL 通配符必须与 LIKE 运算符一起使用!
在 SQL 中,可使用以下通配符:
%替代一个或多个字符、_仅替代一个字符、[charlist]字符列中的任何单一字符、[^charlist]或者[!charlist]不在字符列中的任何单一字符
13.IN 操作符允许我们在 WHERE 子句中规定多个值:
SELECT * FROM Manager.[User] WHERE DivisionID IN (11,13)
14.BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围:
DivisionID在11~13的数据:
SELECT * FROM Manager.[User] WHERE DivisionID BETWEEN 11 AND 13
DivisionID不在11~13的数据:
SELECT * FROM Manager.[User] WHERE DivisionID NOT BETWEEN 11 AND 13
15.通过使用 SQL,可以为列名称和表名称指定别名(Alias):不常用
16.SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据:
引用两个表:
SELECT Manager.[User].DivisionID,Manager.[User].DisplayName,Manager.Division.ID
FROM Manager.[User],Manager.Division
WHERE Manager.[User].DivisionID = Manager.Division.ID
使用JOIN(只展示符合连接条件的数据):<文SQL中INNER JOIN与JOIN是相同的>
SELECT Manager.[User].DivisionID,Manager.[User].DisplayName,Manager.Division.ID
FROM Manager.[User]
INNER JOIN Manager.Division
ON Manager.[User].DivisionID = Manager.Division.ID
ORDER BY Manager.[User].DivisionID
17.不同的SQL JOIN:
特别的:
LEFT JOIN以左表为主表展示符合条件的和主表的所以字段,RIGHT JOIN反之!
如:Table1 T1 JOINTable2 T2 ON 连接条件,其中T1就是左表就是主表
18.UNION 操作符用于合并两个或多个 SELECT 语句的结果集:http://www.w3school.com.cn/sql/sql_union.asp
特别的:
19.SELECT INTO 语句:http://www.w3school.com.cn/sql/sql_select_into.asp
20.CREATE DATABASE 用于创建数据库:http://www.w3school.com.cn/sql/sql_create_db.asp
CREATE DATABASE Smart_Project
21.CREATE TABLE 语句用于创建数据库中的表:http://www.w3school.com.cn/sql/sql_create_table.asp
22.SQL 约束(约束用于限制加入表的数据的类型)
常用约束:
请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束
23.CREATE INDEX 语句用于在表中创建索引:http://www.w3school.com.cn/sql/sql_create_index.asp
24.通过使用 DROP 语句,可以轻松地删除索引、表和数据库:http://www.w3school.com.cn/sql/sql_drop.asp
25.ALTER TABLE 语句用于在已有的表中添加、修改或删除列:http://www.w3school.com.cn/sql/sql_alter.asp
26.Auto-increment 会在新记录插入表中时生成一个唯一的数字:http://www.w3school.com.cn/sql/sql_autoincrement.asp
27.SQL Date 函数:http://www.w3school.com.cn/sql/sql_dates.asp
28.SQL NULL 值:
注意:无法使用比较运算符来测试 NULL 值,比如 =,<,或者 <>,我们必须使用 IS NULL 和 IS NOT NULL 操作符
SELECT * FROM Manager.[User] WHERE Manager.[User].DisplayName is NULL
SELECT * FROM Manager.[User] WHERE Manager.[User].DisplayName is NOT NULL
29.SQL ISNULL()、NVL()、IFNULL() 和 COALESCE() 函数:http://www.w3school.com.cn/sql/sql_isnull.asp
30.SQL 数据类型:http://www.w3school.com.cn/sql/sql_datatypes.asp
31.SQL 函数:http://www.w3school.com.cn/sql/sql_functions.asp
SELECT AVG(DivisionID) AS Average FROM Manager.[User]
查询表中数据数:
SELECT COUNT(*) AS QTY FROM Manager.[User]
返回指定列的不同值的数目:
SELECT COUNT(DISTINCT DivisionID) AS NotSameValue FROM Manager.[User]
SELECT FIRST(DivisionID) AS NotSameValue FROM Manager.[User]
SELECT LAST(DivisionID) AS NotSameValue FROM Manager.[User]
SELECT MAX(DivisionID) AS MAXValue FROM Manager.[User]
SELECT MIN(DivisionID) AS MINValue FROM Manager.[User]
SELECT SUM(DivisionID) AS Amount FROM Manager.[User]
合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句
将相同Password的DivisionID相加并以DivisionAmount列显示:
SELECT Password,SUM(DivisionID) AS DivisionAmount
FROM Manager.[User]
GROUP BY Password
SELECT Password,SUM(DivisionID) AS DivisionAmount
FROM Manager.[User]
GROUP BY Password
HAVING SUM(DivisionID) > 20
SELECT UCASE(DisplayName) AS Capital FROM Manager.[User]
SELECT LCASE(DisplayName) AS Lowercase FROM Manager.[User]
SELECT MID(DisplayName,1,3) AS InDisplayName FROM Manager.[User]
SELECT LEN(DisplayName) AS Length FROM Manager.[User]
SELECT DisplayName,ROUND(DivisionID,-2) AS NewValue FROM Manager.[User]
32.ROW_NUMBER()函数将针对SELECT语句返回的每一行,从1开始编号,赋予其连续的编号:
SELECT
CGUnit.Name AS CGUnitName,
CGItem.Name AS CGItemName,
Currency.Name AS CurrencyName,
ROW_NUMBER() over(order by CGList.ID) as RowID
FROM
Bill.QuotationItem CGList
LEFT JOIN Bill.CGItem CGItem
ON CGList.CGItemID = CGItem.ID
LEFT JOIN Manager.Currency Currency
ON CGList.CurrencyID = Currency.ID
LEFT JOIN Bill.CGUnit CGUnit
ON CGList.CGUnitID = CGUnit.ID
LEFT JOIN Bill.CGSubject CGSubject
ON CGItem.CGSubjectID = CGSubject.ID
WHERE
QuotationID = 71
AND
CGTypeID = 1
33.字符串拼接:
SELECT
CONCAT(CGList.Amount,'/',CGUnit.Name) AS AmountUnit,
FROM
Bill.QuotationItem CGList
LEFT JOIN Bill.CGItem CGItem
ON CGList.CGItemID = CGItem.ID
WHERE
QuotationID = 71
AND
CGTypeID = 2
实战用法:
1.如何使用多个WITH AS?
With XXX as(),YYY as(),ZZZ as()
2.Case When的两种用法?https://www.cnblogs.com/shaopang/p/6903985.html
第一种 格式 : 简单Case函数 :
case 列名
when 条件值1 then 选择项1
when 条件值2 then 选择项2......
else 默认值 end
第二种 格式 :Case搜索函数:
case
when 列名=条件值1 then 选择项1
when 列名=条件值2 then 选择项2.......
else 默认值 end