sql server 基础知识点总结(一)

cast:查询时将列的数据类型转化成其他类型,可以与isnull一起使用
select ISNULL(CAST(gonghao varchar(30),'sdsd')) from ren

解释:将gonghao字段的数据类型转换成varchar(30)类型,在判断gonghao类型是否为空,为空就替换为sdsd

ISNULL:查询,判断如果是null值,则替换为其他

select isnull(name,'sdsd') from ren
解释:判断如果name字段为空,则替换为sdsd

TOP:查询前5行的数据
select top 5 * from goods

查询前百分之50的数据
select top 50 percent * from goods


case:类似于if else语句

实例1:区间用法
select g_ID,g_Name,
    等级=case   
          when g_Price<=2000 then '便宜'
          when g_Price<=4000 then '一般'
          else '奢侈'
       end
  from goods

实例2:等值用法
select g_ID,g_Name,
   等级=case g_Price
        when 1500 then '便宜'
        when 2500 then '一般'
        else '奢侈'
    end
from goods

复制表
select top 5 * into newgoods from goods

exists:包含一个查询语句的查询结果,有结果就返回true,没结果就返回false
if(exists(select * from Goods))
begin
   print('true执行这里');
end
else
begin
   print('false执行这里')
end

row_number():给查询的结果添加一列排序编号
select *,ROW_NUMBER() over(orderby g_id desc) as gnumber from goods

猜你喜欢

转载自blog.csdn.net/a3060858469/article/details/80844700