--建表、插入createtable Person2(Id intnotnull,Name nvarchar(50) notnull,Age intnotnull);insertinto Person2( Id,Name,Age) values(1,'liu',20);insertinto Person2(Name,Age) values('bobo',10);insertinto Person2 values('hehe',20);select NEWID();--插入createtable Person3(ID intnotnull,Name nvarchar(20) null,Age intnotnull);insertinto Person3(ID,Name,Age) values(1,'hhh',20);insertinto Person3(ID,Name,Age) values(2,'hhh',20);insertinto Person3(ID,Name,Age) values(2,'hhh',40);insertinto Person3(ID,Name,Age) values(2,'hhh',30);insertinto Person3(ID,Name,Age) values(2,N'刘春波',30);insertinto Person3(ID,Name,Age) values(2,'刘春波',30);--更新update Person3 set Age=30,Name='liheli'where Age>30or Age<10;update Person3 set Age=30,Name='liheli'where (Age>30and Age=20) or Age<10;update Person3 set Age=30,Name='liheli'where Age<>30;update Person3 set Name='liheli'where Age=40;update Person3 set Age=Age+1;--删除 delete删除数据 drop删除表deletefrom Person3 where Age>20;droptable Person3;
修改表,增加列
altertable Person3 add company nvarchar(20);altertable Person3 add department nvarchar(20);--检索select * from Person3;select Age,Name from Person3;select Age as 年龄 from Person3 where Age=30;select NEWID();select GETDATE();select @@VERSION;select * from Person3;select1+1as 列1,GETDATE() as 日期,NEWID() as 编号;--统计--select count(*) from Person3;selectcount(*) from Person3;selectmax(Age) from Person3;selectmin(Age) from Person3;selectavg(Age) from Person3;selectcount(Age) as 计数 from Person3 where Age=30;--数据排序 order by ASC升序,DESC降序select * from Person3 orderby Age DESC;select * from Person3 orderby Age ASC;select * from Person3 orderby Age desc,Name asc;--先按年龄,年龄相同则按名字select * from Person3 where Age>30orderby Age desc;--通配符查询 -- _单字符匹配,%多字符匹配select * from Person3 where Name like'_h'select * from Person3 where Name like'_h_'select * from Person3 where Name like'__h'select * from Person3 where Name like'_hh'select * from Person3 where Name like'%h'select * from Person3 where Name like'%春%'select * from Person3 where Name like'%'
--sql 中 null 意思为没有,null+1=nullselectnull+1;select''+'123';select'abc'+'123';selectnull+'123';--这两行是查不出来的select * from Person3 where Name<>null;select * from Person3 where Name=null;--sql中要这样查select * from Person3 where Name isnotnull;select * from Person3 where Name isnull;select * from Person3 where Age=21or Age=20;select * from Person3 where Age in (20,21);select * from Person3 where Age between 20and30;--数据分组 group by--按字段分组select Age as 年龄,COUNT(*) as 计数 from Person3 groupby Age;select Name as 姓名,count(*) as 总数 from Person3 groupby Name;select Age as 年龄,AVG(Age) as 平均年龄 from Person3 groupby Age;--Having语句 --having是对分组后的信息过滤,where是对分组前的信息过滤--所以having不能简单地代替whereselect Age as 年龄,count(*) as 计数 from Person3 where Age>20groupby Age;select Age as 年龄,count(*) as 计数 from Person3 groupby Age havingcount(Age)>1;--限制结果集的行数 select top(2) * from Person3 where Age>10orderby Age desc;select top 2 * from Person3 where Age>10orderby Age desc;--下面注意,用in的时候,必须限制后面的语句输出为一列select top 2 * from Person3 where Name notin (select top 1 Name from Person3 orderby Age desc) orderby Age desc;--去掉重复数据select * from Person3;update Person3 set company='beijing'where Name='liu';update Person3 set company='wuhan'where Name='chun';update Person3 set company='zhengzhou'where Name='bo';insertinto Person3 values('bo',21,'beijing');select Name,Age from Person3;--distinct消除完全重复的行selectdistinct Name,Age from Person3
--UNION联合结果集
--union去除了完全重复的数据,只保留一条
--如果不想合并,用unionall
--注意union两边不能有分号
--上下列数要相同(不同用0补足),数据类型要相同
select Name,Age from Person3 where Age>20union --(unionall)--不去除重复结果
select Name,0from Person3 where Age<20;--select Select出来的结果select Name from (select Name from Person3 where Age>15)tempTable;--报表数据select * from Person3;select'最大年龄',max(Age) from Person3
unionallselect'最小年龄',min(Age) from Person3
unionallselect'平均年龄',avg(Age) from Person3
unionallselect'总人数',count(*) from Person3
unionallselect'年龄总和',sum(Age) from Person3
--函数
--数字函数
select abs(-20),abs(10);select ceiling(2.3),ceiling(2.6);select floor(2.3),floor(2.6);select round(2.34,1);--四舍五入,第二位为保留几位小数--字符串含书select len('len');select Name, LEN(Name) as'字符串长度'from Person3;select lower('LEN');select upper('len');select rtrim(' len ');select ltrim(' len ');select len(rtrim(ltrim(' len ')));select substring('lenght',2,2);--从第二位开始,长度为2的子字符串--日期函数select getdate();--当前日期--第一个是单位(可以是,HH,DAY,MOUTH,YEAR等),第二个是数量,第三个是要计算的日期select dateadd(DAY,-10,getdate());--差额计算,第一个是单位,第二个是开始日期,第三个是结束日期select datediff(DAY,getdate(),dateadd(day,-10,getdate()));altertable Person3 add Indate datenull ;select * from Person3;update Person3 set Indate=dateadd(YEAR,-10,getdate()) where Age=21;update Person3 set Indate=dateadd(YEAR,-20,getdate()) where Age=12;update Person3 set Indate=dateadd(YEAR,-12,getdate()) where Age=20;select Name as 姓名,Indate as 入职日期,datediff(YEAR,Indate,getdate()) as 入职时间 from Person3;select datediff(YEAR,Indate,getdate()) as 入职时间,count(*) as 计数 from Person3 groupby datediff(YEAR,Indate,getdate());--去日期的特定部分select datepart(YEAR,getdate()),datepart(MONTH,getdate());select datepart(year,Indate) as 入职年份,count(*) as 计数 from Person3 groupby datepart(year,Indate);--类型转换--字符串转整形,字符串转日期selectcast('123'asint),cast('2008-8-8'as datetime);select convert(datetime,'2008-8-8'),convert(varchar(50),123);--空值处理函数select * from Person3;select company from Person3;select isnull(company,'佚名') from Person3;--CASE判断createtable TempTable(ID intnotnull,Name nvarchar(20) null,VIP intnull);insertinto TempTable(ID,Name,VIP) values (1,'liu',1) ,(2,'chun',2), (3,'bo',3);select * from TempTable;select Name,
(
case VIP
when1then'普通客户'when2then'会员'when3then'VIP'else'未知客户类型'end
)
as 客户类型 from TempTable;select * from Person3;select Name,
(
casewhen Age<20then'少年'when Age>20and Age<30then'青年'end
) from Person3
--建立子查询
select * from (select * from Person3)t;select * from (select * from Person3) as t;
学习SQL语句时候练习记录,仅供参考--建表、插入create table Person2(Id int not null,Name nvarchar(50) not null,Age int not null);insert into Person2( Id,Name,Age) values(1,'liu',20);insert into Person2(Name,Age) values(