首页 > 代码库 > SqlSever 查询基本
SqlSever 查询基本
查询语句:
SQL sever 查询语句;
1、查询所有字段:
select * from UserInfo
2、条件筛选
(如查询UserInfo中的UserName)
select UserName from UserInfo
where语句:
select * from UserInfo where sex=‘男‘ and Major=‘计算机’ (and/or)
top语句: top 5* ... ... order by ... ... desc(desc标示降序——不写就是升序) 注意(数字加* ) brder by
select top 3* from UserInfo where Major=‘计算机‘ order by Age desc
Max: 查询最大的数值 (如查询出年龄字段Age最大的)
select Max(Age) from UserInfo
-----最小用Min 平均 Avg
SUM:总和(如下,查询Age总和)
select SUM(Age) from UserInfo
count:数量(如在所有数据中查找出计算机专业的学生数量)
select count(*) from UserInfo where Major=‘计算机‘
as:给查询出来的字段起个别名,方便以后使用(如将以上查询结果命名为 UserCount)
select count(*)as UserCount from UserInfo where Major=‘计算机‘
group by :查询结果分组() having count(*)>3(此时不能用where)
select count(*) as GroupCount,Major from UserInfo group by Major having count(*)>3
like :模糊查询
select * from UserInfo where ReaName like ‘%张%‘
SqlSever 查询基本