SQL server 02

--找到指定字符出现在字符串中的位置
--金蝶
select CHARINDEX('蝶',SName,0) from    t_Student
--输出当前字符(varchar   nvarchar   char    nchar  text  ntext)的长度
--在存储的方法  一个汉字  占2个字节 但是长度和存储无关
select  LEN(sname),sname  from t_Student

select   UPPER('a')
select   LOWER('A')

--去除左右空格
select LTRIM(RTRIM(SName)),SName from t_Student   order by SName 


--替换
select  REPLACE(SName,' ','') from t_Student   order by SName 


--获取数据库中的当前时间=系统时间
select   GETDATE()

--yyyy-mm-dd hh:MM:ss
select DATEADD(HH,-10,GETDATE())

--查询2个月份之间的绝对相差值
select DATEDIFF(DD,'2019-01-01',GETDATE())


--返回当前星期x
select  DATENAME(DW,getdate())

--随机数
select  RAND()*10000
--向上取整  或者   =整数
select CEILING(RAND()*10000)
--向下取整=整数
select  FLOOR(RAND()*10000)
--四舍五入
select   ROUND(RAND()*10000,0)

--获取当前用户名
select  CURRENT_USER

--获取到计算机名称
select  HOST_NAME()

--数据类型的转换
select  CONVERT(varchar(60),123)
--在将 varchar 值 'abc' 转换成数据类型 int 时失败。
select  CAST('abc' as int)

--模糊查询
--like  
--select  * from   table  where   条件列  like   '%值%'  
--包含   like 中的值
select * from t_Student  where   SAddress   like '%天%'

--以什么打头
--select  * from   table  where   条件列  like   '值%'  
select * from t_Student  where   SAddress   like '天%'

--以什么结尾
--select  * from   table  where   条件列  like   '%值'  
select * from t_Student  where   SAddress   like '%区'

--查找以什么打头 并且长度可以设置
--  下划线 代表一个字符
--select  * from   table  where   条件列  like   '值_'  
--夏一桐
select    *  from  t_Student where sname  like '金_'
select    *  from  t_Student where sname  like '夏__'

select  * from  t_Student



insert into  t_Course 
select  NEWID(),'C# Base'   union
select  NEWID(),'SQL SERVER 2014'   union
select  NEWID(),'Winfrom'   union
select  NEWID(),'C# 4.5 语法糖'   union
select  NEWID(),'ASP.NET(WEBFROM)'   union
select  NEWID(),'ASP.NET MVC'   union
select  NEWID(),'jQuery+WCF'   union
select  NEWID(),'ASP.NET MVC+jQuery   Easy UI'   union
select  NEWID(),'一般应用程序'   


--金蝶
--洛飞
--凌辉
--白燕
--夏一桐--欧阳燕飞
select   * from  t_Score   


insert  into t_Score   
select  
NEWID(),
(select Scode   from t_Student  where SName='金蝶'),
(select CourseID    from   t_Course  where  CourseName='Winfrom'),
30    union
select  
NEWID(),
(select Scode   from t_Student  where SName='洛飞'),
(select CourseID    from   t_Course  where  CourseName='Winfrom'),
86     union
select  
NEWID(),
(select Scode   from t_Student  where SName='凌辉'),
(select CourseID    from   t_Course  where  CourseName='Winfrom'),
77   union
select  
NEWID(),
(select Scode   from t_Student  where SName='白燕'),
(select CourseID    from   t_Course  where  CourseName='Winfrom'),
44    union
select  
NEWID(),
(select Scode   from t_Student  where SName='夏一桐'),
(select CourseID    from   t_Course  where  CourseName='Winfrom'),
68   

-- 》=值 and   《值
select  * from  t_Score  where    Score  between   55  and   87
----in    where   条件列=条件值  or 条件列=条件值2  or 条件列=条件值3  
select  * from t_Student  where SAddress  in ('天津市河西区','天津市南开区','北京市朝阳区大屯')
----not in    where   条件列<>条件值  or 条件列<>条件值2  or 条件列<>条件值3  
select  * from t_Student  where SAddress  not in ('天津市河西区','天津市南开区','北京市朝阳区大屯')
--聚合函数   
--1 最大数   max
select  max(Score) from t_Score
--2 最小数   min
select  min(score) from t_Score
--3 平均数  avg
select  avg(score) from t_Score
--4 总和     sum
select  sum(score) from t_Score
--5 总数量  count(主键列)   不要使用可以为空的列  如果 使用为空的列  那么将不记录数
select   COUNT(*) from  t_Score
select  COUNT(*) from  t_Student
select  COUNT(SEmail) from  t_Student
select  COUNT(Scode) from  t_Student

--分组 出来的数据数据 是一个和数据表 完全不相同的数据结果集
--分组查询 就会产生一个新的数据结果集
--分组  一定会用到 聚合函数,分组的列就是查询的列,没有查询的列 是不能够作为分组的列
--聚合函数所产生的列是一个虚拟的列,当和表中实际列同时使用的时候,必须使用分组
--查询每个年级的人数
select  COUNT(Scode),SGrade from   t_Student  group   by SGrade
--查询每个年级的人数  只查询男的
select COUNT(Scode),SGrade from   t_Student   where SSex='男'  group   by SGrade
--根据姓名排序 然后分出男女
select COUNT(Scode),SSex,SName from   t_Student group by ssex,SName order by sname 

select COUNT(Scode),SSex from   t_Student group by ssex


--查询所在年级总人数低于15个人
select COUNT(Scode),SGrade from   t_Student group by SGrade    having COUNT(Scode)<15
--查询所在年级总人数高于15个人
select COUNT(Scode),SGrade from   t_Student group by SGrade    having COUNT(Scode)>14
--查询每门课程的平均分,并且按照分数由低到高的顺序排列显示
select   AVG(score),CourseID  from t_Score group by CourseID   order by AVG(Score) asc
select   AVG(score),CourseID  from t_Score group by CourseID   order by AVG(Score) desc
--连接查询分为2大类
--内查询   1v1关系  主表和外表里面都有数据 才能够进行显示
--2
select *      from  t_Course
select * from t_Score

select  c.CourseName,s.Score  from t_Score  as  s
inner  join t_Course  as c
on s.CourseID=c.CourseID


select  t_Student.SName,t_Score.Score from  t_Student
inner   join  t_Score
on t_Student.Scode=t_Score.StudentID

--3

select  t_Student.SName,t_Score.Score,t_Course.CourseName from  t_Student
inner   join  t_Score  on t_Student.Scode=t_Score.StudentID
inner join t_Course   on  t_Score.CourseID=t_Course.CourseID


select  t_Student.SName,t_Score.Score,t_Course.CourseName from  t_Student
inner   join  t_Score  on t_Student.Scode=t_Score.StudentID
inner join t_Course   on  t_Score.CourseID=t_Course.CourseID
order   by  t_Student.SName 
--外查询
--左外     已左边的为主表,显示左边表所有的内容 如果子表中不存在记录 则自动填充为NULL
select  t_Student.SName,t_Score.Score from  t_Student
left   join  t_Score
on t_Student.Scode=t_Score.StudentID

select  t_Student.SName,t_Score.Score,t_Course.CourseName from  t_Student
left   join  t_Score  on t_Student.Scode=t_Score.StudentID
left join t_Course   on  t_Score.CourseID=t_Course.CourseID
order   by  t_Student.SName 
--右外 

猜你喜欢

转载自blog.csdn.net/dl990813/article/details/96883337