该文章所用数据库仅供参考,语法适用使有数据库。
第1节. 查询数据
在数据库中,有很多个数据库,数据库里面有很多个表,表里面有很多的行跟列,在表中,每行代表一个唯一记录,每列代表记录中的一个字段。
如果我们要从这些表中查询数据,就要用到select语句。select语句的基本语法:
SELECT
select_list
FROM
schema_name.table_name;
在上面语法中:
- select_list 指定需要的列,多个列用逗号分隔。
- schema_name.table_name 在from子句中指定的表及其模式。
第2节. 对数据进行排序
使用select语句进行查询的时候,查出来的结果集都没有指定顺序的数据,所以需要把结果集进行排序,以便我们查看,就要用order by 字句进行排序,order by 字句的语法:
SELECT
select_list
FROM
table_name
ORDER BY
[column_name | expression] [ASC | DESC ]
在上面语法中,
- column_name | expression 指定要对查询结果集进行排序的列名或表达式。
- ASC | DESC 指定列中的值按升序排序还是按降序排序。ASC升序/DESC降序
1. 按升序对结果集进行排序
select customer_id,first_name,last_name
from sales.customers
order by first_name
2.按降序对结果集进行排序
select customer_id,first_name,last_name
from sales.customers
order by first_name desc
3.按多列对结果集进行排序
select first_name,last_name,city
from sales.customers
order by city,first_name
4.按多列和不同顺序对结果集进行排序
select first_name,last_name,city
from sales.customers
order by city asc,first_name
5.按不在选择列表中的列对结果集进行排序,
SELECT
city,
first_name,
last_name
FROM
sales.customers
ORDER BY
state;
state不在表sales.customers中,但是还是可以进行排序。即使state列不显示在选择列中。
6. 按表达式对结果集进行排序
SELECT
first_name,
last_name,
LEN(first_name) as len_name
FROM
sales.customers
ORDER BY
LEN(first_name) DESC;
7. 按列的序数位置排序
SELECT
first_name,
last_name
FROM
sales.customers
ORDER BY
1,
2;
第3节. 限制返回行数
1. SQL Server Select Top语句
Select top 子句用于限制查询结果集中返回的行数或行百分比。
由于存储在表中的行的顺序是不可预测的,因此 SELECT TOP 语句始终与 ORDER BY 子句一起使用。 结果,结果集限于前 N 个有序行数。
Select Top的语法:
SELECT TOP (expression) [PERCENT]
[WITH TIES]
FROM
table_name
ORDER BY
column_name;
在此语法中,select语句可以包含其他子句,如:where,join,having和group by。
- Expression 指定返回的行数。
- PERCENT 返回前 N 个行百分比,其中 N 是表达式的结果。
- WITH TIES 用于返回更多行,其值与有限结果集中的最后一行匹配。返回的行数多于在表达式中返回的行数。
1.查出前10行
select top 10 first_name,last_name,city
from sales.customers
order by 1,3
;
2.查出前10%行
select top 10 percent first_name,last_name,city
from sales.customers
order by 1,3
;
3.把10行最后一行的数据再找出来
select top 10 with ties product_name,list_price
from production.products
order by list_price
;
2. SQL Server Offset Fetch子句
OFFSET 和 FETCH 子句是 ORDER BY 子句的选项。 它们用于限制查询返回的行数。必须将 OFFSET 和 FETCH 子句与 ORDER BY 子句一起使用
OFFSET 和 FETCH 子句比实现 TOP 子句更适合实现查询分页解决方案
...
ORDER BY column_list [ASC |DESC]
OFFSET offset_row_count {ROW | ROWS}
FETCH {FIRST | NEXT} fetch_row_count {ROW | ROWS} ONLY
在上面语法中,
OFFSET 子句指定在开始从查询返回行之前要跳过的行数。 offset_row_count 可以是大于或等于
零的常量,变量或参数。
FETCH 子句指定在处理 OFFSET 子句后要返回的行数。 offset_row_count 可以是大于或等于 1 的 常量,变量或标量。
OFFSET 子句是必需的,而 FETCH 子句是可选的。 此外, FIRST 和 NEXT 是同义词,因此可以互换使用它们。
1.跳过前 10 个数据并选择接下来的 10 个数据
select product_name,list_price
from production.products
order by list_price
offset 10 rows
fetch next 10 rows only
2.跳过前 10 个数据返回其他数据
select product_name,list_price
from production.products
order by list_price
offset 10 rows
3.分页
num 页数 size 每页多少条
select product_id,list_price
from production.products
order by product_id
offset (num-1)*size rows
fetch next size rows only
第4节.过滤数据
1. Select Distinct子句
Select Distinct子句查找所有不同的语句,Distinct子句将使有NULL值视为相同的值。
语法:
SELECT DISTINCT
column_name1,
column_name2 ,
...
FROM
table_name;
示例:
select distinct first_name,last_name
from sales.customers
order by first_name;
select distinct *
from sales.customers
order by first_name;
select distinct phone
from sales.customers
order by 1;
A. DISTINCT一个字段的示例
以下语句返回 customers 表中所有客户所在的所有城市:
SELECT
city
FROM
sales.customers
ORDER BY
city;
B. DISTINCT多列示例
以下语句查找所有客户的不同城市和州。
SELECT DISTINCT
city,
state
FROM
sales.customers
C. DISTINCT带有null值示例
以下示例查找客户的不同(唯一)电话号码:
SELECT DISTINCT
phone
FROM
sales.customers
ORDER BY
phone;
2. SQL Server WHERE子句
条件语句where,如果需要获取满足条件的就需要使用where,获取满足一行或多个条件的行。
Where语法:
SELECT
select_list
FROM
table_name
WHERE
search_condition;
在where中,会按照条件进行过滤from子句返回的行,WHERE 子句仅返回导致搜索条件计算为TRUE 的行。搜索条件是逻辑表达式
示例:
select *
from sales.customers
where phone is null
select *
from sales.customers
where phone ='(916-6003)'
3. AND 查找满足两个条件的行
AND 是一个逻辑运算符,用于组合两个布尔表达式
示例:
select *
from sales.customers
where state ='CA'
and city = 'Campbell';
select *
from sales.customers
where state ='CA'
and city = 'Campbell'
and zip_code = 95008;
4. (>,=,<)使用比较运算符
示例:
(>,=,<)使用比较运算符查找行
select * from production.products
where list_price >= 3000
and list_price < 5000
5. OR 查找满足任意一个条件的行
OR 是一个逻辑运算符,用于组合两个布尔表达式
--OR 查找满足两个条件中的任何一个的行
--OR 是一个逻辑运算符,用于组合两个布尔表达式
---------------------------------------
--A. 使用OR运算符示例
select * from production.products
where list_price <= 3000
or list_price >= 5000
---------------------------------------
--B. 使用OR和AND运算符示例
select * from production.products
where list_price <= 3000
or list_price >= 5000
and model_year = 2017
select * from production.products
where list_price <= 3000
or list_price >= 5000
and model_year = 2017
order by list_price
select * from production.products
where ( list_price <= 3000
or list_price >= 5000 )
and model_year = 2017
order by list_price
select * from production.products
where list_price <= 3000
or ( list_price >= 5000
and model_year = 2017 )
order by list_price
select * from production.products
where list_price >= 3000
and list_price <= 5000
order by list_price;
6. Between查找两个值之间的值的行
BETWEEN 运算符是一个逻辑运算符,用于指定要测试值的范围。
--Between查找具有两个值之间的值的行
select * from production.products
where list_price between 3000 and 5000;
7. IN 查找值列表中具有值的行
IN 运算符是一个逻辑运算符
IN 运算符等效于多个 OR 运算符,因此,以下语法是等效的:
column IN (v1, v2, v3)
column = v1 OR column = v2 OR column = v3
要取消 IN 运算符,请使用 NOT IN 运算符,如下所示:
column | expression NOT IN ( v1, v2, v3, ...)
示例:
--IN 查找值列表中具有值的行
select * from production.products
where list_price = 4499.99
or list_price = 3499.99
or list_price = 3599.99;
select * from production.products
where list_price in (4499.99,3499.99,3599.99);
8. Like 查找包含指定字符串的行
pattern
模式是要在列或表达式中搜索的字符序列。它可以包含以下有效通配符:
通配符百分比( % ):任何零个或多个字符的字符串。
下划线( _ )通配符:任何单个字符。
[list of characters] 通配符:指定集合中的任何单个字符。
[character-character] :指定范围内的任何单个字符。
[^] :不在列表或范围内的任何单个字符。
like模糊查询默认是不区分大小写的
通配符使 LIKE 运算符比等于( = )和不等于( != )字符串比较运算符更灵活。
--Like 查找其值包含字符串的行
--A. %(百分比)通配符
查找a开头的客户:
select * from sales.customers
where fist_name like 'a%'
查找er结尾的客户:
select * from sales.customers
where fist_name like '%er'
---------------------------------------
--B. _(下划线)通配符
查找第二个字母为‘A’的客户:
select * from sales.customers
where fist_name like '_A%'
select * from sales.customers
where fist_name like '__'
select * from sales.customers
where fist_name like '_a_'
or first_name like '_b_'
---------------------------------------
--C. [list of characters]通配符
查询第一个字母为abcde其中一个的客户:
select * from sales.customers
where fist_name like '[abcde]%';
---------------------------------------
--D. [character-character]通配符
查询第一个字符是范围a-c的字母:
select * from sales.customers
where fist_name like '[a-c]%';
---------------------------------------
--E. [[^]]通配符
查询第一个字符不是范围a-c的字母:
select * from sales.customers
where fist_name like '[^a-c]%';
select * from sales.customers as c
where c.fist_name like '[^a-c]%'
---------------------------------------
--F. NOT LIKE运算符
查询第一个字符不是范围a-c的字母:
select * from sales.customers
where fist_name not like '[a-c]%';
-------------------------------------------------
9.列和表别名
别名用于为表或表中的列提供临时名称。
select fist_name as 姓氏,last_name 名称
from sales.customers as c
order by 姓氏