标签:salary PersonInfos Server Sql 基本操作 null where id select
Sql Server数据库基本操作
已有如下表
其中id列为主键,且自动递增1。在sql语句中是不区分大小写的。
一、增加
说明:主键列是自动增长,在增加插入的时候可以忽略不计。字符串需要使用单引号。
insert into PersonInfos
values('1998-9-10','巴拉巴拉','balabala...',10000.99)
如果插入的时候允许为null,也可以插入null。还可以一次性插入多行数据,括号之间使用逗号隔开。
insert into PersonInfos
values('1998-9-10','巴拉巴拉','balabala...',10000.99),('9999-9-10','巴拉巴拉啦啦啦','balabala...',null)
二、修改
update PersonInfos
set cnname='张含韵',salary=0
where id=5
上句中修改了id=5的两列信息,中间用逗号隔开。where后面的条件语句也可以更复杂,在后的查询中再细讲。
三、删除
delete from PersonInfos
where id>=4
四、查询
1、查询所有列
select * from PersonInfos
使用*代表所有列。
2、查询指定列
select birthday,cnname from PersonInfos
有多列的时候,中间用逗号隔开。
3、加条件查询(where)
条件语句where后面支持多种运算符
3.1、比较运算符
支持 =、>、<、<=、>=和!=
select * from PersonInfos
where id>1
select * from PersonInfos
where cnname='张含韵'
3.2、逻辑运算符
支持and、or和not
select * from PersonInfos
where id>2 and birthday>'2000-01-01'
3.3、模糊查询
like,%表示任意多个任意字符,_表示一个任意字符
select * from PersonInfos
where cnname like '张%'
select * from PersonInfos
where cnname like '张__'
3.4、范围查询
in表示再一个非连续的范围内(是这个非连续中的某一个值),not in也是支持的。
select * from PersonInfos
where id in(2,4,6)
select * from PersonInfos
where id not in(1,2,6)
between…and…表示在一个连续的范围内
select * from PersonInfos
where birthday between '1995-01-01' and '2000-12-30'
3.5、空判断
select * from PersonInfos
where salary is null
select * from PersonInfos
where salary is not null
4、排序(order by)
asc:升序(ascend)
desc:降序(descend)
select * from PersonInfos
where salary is not null
order by salary desc,birthday asc
5、Top
select top 3 * from PersonInfos
where salary is not null
order by salary desc
薪水前3。
6、聚合函数
6.1、总数
count(*)表示计算总行数,括号中写星与列名结果是相同的
select count(*) from PersonInfos
where salary is not null
6.2、最大数和最小值
max(列) 表示求此列的最大值
select MAX(salary)
from PersonInfos
where salary is not null
min(列) 表示求此列的最小值
select min(salary)
from PersonInfos
where salary is not null
6.3、求和与平均值
avg(列) 表示求此列的平均值
select avg(salary)
from PersonInfos
where salary is not null
sum(列) 表示求此列的和
select sum(salary)
from PersonInfos
where salary is not null
标签:salary,PersonInfos,Server,Sql,基本操作,null,where,id,select 来源: https://blog.csdn.net/zhudaokuan/article/details/112628503
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。