SQL server-数据库的查询(初级)

本文介绍了SQL Server中进行数据查询的基础操作,包括选择特定列、去除重复数据、使用TOP关键字、排序、条件过滤、别名设定、值替换、NULL处理、区间查询、模糊匹配以及如何将查询结果存入新表。还探讨了内连接、左外连接和多表查询等进阶查询技巧,帮助读者掌握SQL Server的数据查询能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--数据查询(结果集) 简单查询
--语法 select 列名1,列名2,.. from 表名

select * from [dbo].[Product]--*  所有

--数据去重 distinct
--查询客户地址簿表中的收货客户都来自哪些国家。

select distinct Country from [dbo].[Address]

--查询商品表中前10条数据 top关键词

select top 10 * from Product

--查询商品表中批发价最贵的商品 排序 order by 列名 默认升序(asc) 降序 desc

select top 1 * from Product order by UnitPrice desc

--查询商品表中前10%数据

select top 10 percent  * from Product

--条件 where 
--查询点击量小于 100 的咨询。

select * from [dbo].[News] where Clicks<100

--查询商品表(Product)中品牌(Brand)为“漂流木或 OSA”的商品信息

select * from [dbo].[Product] where Brand='漂流木' or Brand='OSA'

--优化上条语句

select * from [dbo].[Product] where Brand in('漂流木','OSA')

--查询收获地址所在城市为"北京、上海、郑州、济南"的所有客户。

select * from [dbo].[Address] where City in('北京','上海','郑州','济南')

--列的别名
--查询商品表中商品的编号、名称、进货价信息

select id as 编号,name 名称,进货价=UnitPrice from Product 

--值替换
--语法  case when 旧值  then 新值 end 
--查询商品表中商品编号、名称,有一列canuse表示商品是否上架 是=已上架 否=未上架
--select id,name,
--状态=case CanUse
--when '是' then '已上架'
--when '否' then '未上架'
--end
-- from Product

select id,name,
状态=case CanUse
when '是' then '已上架'
else '未上架'
end
 from Product

--查询客户电话为空的客户信息   null   is 

 select * from Customers where MPhone is null

  --查询客户电话不为空的客户信息 

  select * from Customers where MPhone is not null

  --介于区间 列名 between 最小值 and 最大值
  --查询商品进货价再50-100元之间的商品信息

  select * from Product where UnitPrice>=50 and UnitPrice<=100

  --优化

  select * from Product where UnitPrice between 50 and 100

  --查询1990年出生的且电话为空的客户信息

  select * from Customers where Birthday between '1990-1-1' and '1990-12-31' and MPhone is null

  --字符模糊匹配   列名 like'值'
  --% 匹配一个或多个
  -- _ 匹配一个 
  --查询姓张的客户信息

 select * from Customers  where RealName like'张%'

  --查询名字中含有爱字的客户信息

 select * from Customers  where RealName like'%爱%'

 --查询的结果集放入一张新表中

  select id,name into KHXX from Product
  select * from KHXX

  --以上全部为基础部分
  --查询商品分类下的商品信息,要求显示商品分类名及商品信息

  select pc.Name,p.* from ProductCategory pc join Product p
  on pc.Id =p.CategoryId

 --语法 内连接  select 列名 from 表1 [inner] join 表2 on 表1.id=表2.uid 
  --查询客户都有哪些收货地址要求,客户姓名、生日、收货地址、

  select c.RealName,c.Birthday,a.Province,a.City,a.Address from Customers c join Address a
  on c.UserId=a.BelongToUserId

--查询每种商品分类下的商品信息,就算商品分类下没有商品,也要显示商品分类
  --左外连接 left join   以join 左边表为主表,显示所有,右边表为从表去匹配,有就显示,没有以null显示

 select pc.Name,p.* from ProductCategory pc left join Product p
  on pc.Id= p.CategoryId

--查询客户下了哪些订单,要求把所有客户(客户名字)显示出来
  --右外连接

  select c.RealName,o.* from Orders o right join  Customers c
  on c.UserId= o.CustomerId

  --完全连接

 select c.RealName,o.* from Orders o full join  Customers c
  on c.UserId= o.CustomerId

  --查询商品分类下的商品类型要求显示 商品分类编号、商品分类名、商品父分类编号、商品父分类名

 select pc1.id,pc1.Name,pc2.ParentId,pc2.Name from ProductCategory pc1 join ProductCategory pc2
  on pc1.Id=pc2.ParentId

 --多表查询
  --查询商品名及商品的库存信息

  select p.name,ps.* from Product p join ProductPic pp
  on p.Id = pp.ProductId join ProductStore ps
  on pp.Id = ps.ProductPicId

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值