SQLServer学习札记(2)

SQLServer学习笔记(2)
--日期函数

select getdate() as 当前时间

/*

年份

 yy、yyyy

 

季度

 qq、q

 

月份

 mm、m

 

每年的某一日

 dy、y

 

日期

 dd、d

 

星期

 wk、ww

 

小时

 hh

 

分钟

 mi、n

 

秒

 ss、s

 

毫秒

 ms

 

 

 

*/

select dateadd(hh,2,getdate())

select datediff(yy,'1983-08-23',getdate())

--返回时间内容

--datename返回字符串

select datename(dw,getdate())

--datepart 返回整数

select datepart(dw,getdate())-1

 

--数学函数

select floor(78.9)

select ceiling(78.1)

select round(78.1,1)--注意下  后面的参数可以是正数/负数,正数表示小数点后(可以理解为有效数字的位数)    ,在前面就是看与的关系,大于前位加, 小于自身变

--系统函数

--convert  强制转换

select convert(int,34.1)

select convert(varchar(20),12334567)

--对时间的格式化(word文档)

select CONVERT(varchar(12) , getdate(), 111 )

--得到登陆的用户名

SELECT CURRENT_USER

select user_name(1)

SELECT SYSTEM_USER

 

--案例分析

 

create table card

(

  id int identity primary key, --建立表识列并设立主键

  password varchar(10) not null

 

)

insert into card values ('0oi1o1')

 select * from card

 

select replace(password,'o','0') from card

 

select replace(password,'i','1') from card

update card set password=replace(replace(password,'o','0'),'i','1')

 

--案例二

create table sellrecord

(

  id int identity primary key, --建立表识列并设立主键

  listnumber varchar(10) not null

 

)

 

insert into sellrecord values('106-18')

select * from sellrecord

select left(listnumber,2)as 前半部分from sellrecord

--找通式

select charindex('-',listnumber,1)-1 from sellrecord

 

select left(listnumber,charindex('-',listnumber,1)-1) as 前半部分from sellrecord

 

select stuff(listnumber,1,charindex('-',listnumber,1),'') from  sellrecord

 

select right(listnumber,2)as 后半部分 from sellrecord

--最后

select * from sellrecord order by convert(int, left(listnumber,charindex('-',listnumber,1)-1)),convert(int,stuff(listnumber,1,charindex('-',listnumber,1),''))

 

--模糊查询

--like

use StuDB

select * from Score

select * from Score where SName like '华%'

select * from Score where SScore like '8[0-9]'

--between --and

select * from Score where SScore>80 and SScore<90

select * from Score where SScore between 80 and 90

select * from Score where SScore between 80 and 90

--in 表示在该范围内

select * from Score where SScore in (80,30)

select * from Score where SScore not in(80,30)

 

--is null  / is not null

 

--对学生成绩的调查

select * from Score

select sum(SScore) as 班级总分,

 avg(SScore) as 平均分,

 count(*) as 需要参考人数,

 count(SScore) as 参考人数,

 count(*)-count(SScore)  缺考人数,

  min(SScore) 最低分,

 max(SScore) 最高分

 from Score

 

--注意一点: 有null记录时候

delete Score where ID=9

 

…………………………………………………………………………………………………………………………

 

 

 

--查询分析器中执行:

--建表table1,table2:

create table table1(id int,name varchar(10))

create table table2(id int,score int)

insert into table1 select 1,'lee'

insert into table1 select 2,'zhang'

insert into table1 select 4,'wang'

insert into table2 select 1,90

insert into table2 select 2,100

insert into table2 select 3,70

select * from table1;

select * from table2

-------------------------------------------------

 

 

--一、外连接

--1.概念:包括左向外联接、右向外联接或完整外部联接

 

--2.左连接:left join 或left outer join

--(1)左向外联接的结果集包括LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。

--(2)sql语句

select * from table1 left join table2 on table1.id=table2.id

 

 

------------------------------

--注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示

 

--3.右连接:right join 或right outer join

--(1)右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。

--(2)sql语句

select * from table1 right join table2 on table1.id=table2.id

 

-------------结果-------------

 

------------------------------

--注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示

 

--4.完整外部联接:full join 或full outer join

--(1)完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。

--(2)sql语句

select * from table1 full join table2 on table1.id=table2.id

 

 

------------------------------

--注释:返回左右连接的和(见上左、右连接)

 

--二、内连接

--1.概念:内联接是用比较运算符比较要联接列的值的联接

 

--2.内连接:join 或inner join

 

--3.sql语句

select * from table1 join table2 on table1.id=table2.id

select * from table1 join table2 on table1.id>table2.id

select * from table1 join table2 on table1.id!=table2.id

 

 

--注释:只返回符合条件的table1和table2的列

 

--4.等价(与下列执行效果相同)

--A:

select a.*,b.* from table1 a,table2 b where a.id=b.id

--B:

select * from table1 cross join table2 where table1.id=table2.id  --(注:cross join后加条件只能用where,不能用on)

 

--三、交叉连接(完全)

xxd

--1.概念:没有WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生*3=9条记录)

 

--2.交叉连接:cross join (不带条件where...)

 

--3.sql语句

select * from table1 cross join table2