Sql Server

本文介绍了使用SQL进行数据检索的基本语法,包括单列和多列数据检索、利用DISTINCT关键字去除重复记录、使用TOP关键字限制结果数量、ORDER BY进行排序、WHERE子句过滤特定条件的数据等实用技巧。

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

--检索单列数据
select prod_name from Products;
--检索多列数据
select prod_name,prod_price,prod_id from Products
--检索所有列
select * from Products
--DISTINCT关键字
select distinct vend_id from Products
--限制结果
select top 5 prod_name from Products
--排序数据
select prod_name from Products order by prod_name
--按多个排序
select prod_name,prod_price,prod_id from Products order by prod_price,prod_name;
--指定排序方向
select prod_id,prod_price,prod_name from Products order by prod_price asc
--过滤数据
select prod_name,prod_price from Products where prod_price=3.49;
select prod_name,prod_price from Products where prod_price<10;
select vend_id,prod_name from Products where vend_id <> 'DLL01';
select prod_name,prod_price from Products where prod_price between 5 and 10;
select prod_name from Products where prod_price is null;
select prod_id,prod_price,prod_name from Products where vend_id='DLL01'and prod_price<=4;
select prod_name,prod_price from Products where vend_id in('DLL01','BRS01')order by prod_name;
select prod_name,vend_id from Products where not vend_id='DLL01'order by prod_name
--通配符
select prod_id,prod_name from Products where prod_name like '%bean bag%'
select prod_id,prod_name from Products where prod_name like '_ inch teddy bear';
select cust_contact from Customers where cust_contact like '[JM]%'
--拼接字段
select vend_name+'('+vend_country+')' from Vendors order by vend_name;
select RTRIM(vend_name)+'('+RTRIM(vend_country)+')'as vend_title from Vendors order by vend_name;
select prod_id,quantity,item_price,quantity*item_price as expanded_price from OrderItems where order_num=20008;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值