一、基础查询
(1)查看列
select 列名称
[into 新表名]
from 表名称
[where 查询条件]
[group by 分组表达式]
[having 分组条件]
[order by 列名 [asc/desc]]
(2)使用计算列
#计算商品表所有价格的和
select sum(price)
from 商品表
#计算商品表所有价格个数
select count(price)
from 商品表
#将商品表所有价格乘以1.05
select price*1.05
from 商品表
#计算商品表中库存数量
select (进货数量-销售数量) as 库存数量
from 商品表
#计算商品表中商品利润
select (商品单价-商品进价)*销售数量 as 商品利润
from 商品表
(3)使用distinct关键字
select distinct 列名称
from 表名称
(4)使用where子句
select 列名称
from 表名称
where 条件表达式
其中,条件表达式可使用:
- 关系运算符
- 逻辑运算符
- 范围运算符
- 列表运算符
- 模糊查询
select 字段
from 表
where 某字段 like 条件
-
空值运算符
(5)order by语句 -
单条件排序
-
多条件排序
(6)使用TOP关键字
select top n 列名
from 表名
select top n percent 列名
from 表名