【网学网提醒】:网学会员为广大网友收集整理了,sql语句学习代码,希望对大家有所帮助!
SELECT*FROMC
--LIKE关键字的使用
--1.0字原
SELECTCnoFROMCWHERECnameLIKE'P_SCAL';
INSERTINTOCVALUES('9','PCSCAL','8',4);
--2.0使用近似,即以SCAL结束的字符串皆可以满足
SELECTCnoFROMCWHERECnameLIKE'%SCAL'
--以P开始的字符串都可以满足
SELECTCnoFROMCWHERECnameLIKE'P%'
--中间含有SCA字符串的皆可以作为结果
SELECTCnoFROMCWHERECnameLIKE'%SCA%'
--查询结果的排列
--1.0查询结果倒序排列
SELECT*FROMCORDERBYCnoDESC
--查询结果顺序排列,一般默认就是正序排列
SELECT*FROMCORDERBYCnoASC
--统计查询结果的行数
SELECTCOUNT(Cname)TOTAL_CNAMEFROMCWHERECpnoISNOTNULL;
SELECTCOUNT(DISTINCTCcredit)FROMCWHERECpnoISNOTNULL;
--GROUPBY指令,对栏位中的同名栏位进行分组,同名栏位进行某种计算,(求和,平均值等等)
CREATETABLEStore_Information
(
store_namevarchar(25)primarykey,
salesintnotnull,
datedatetime
);
select*fromStore_Information;
insertintoStore_Informationvalues('LosAngeles',1500,1-05-1999);
insertintoStore_Informationvalues('SanFrancisco',300,1-05-1999);
insertintoStore_Informationvalues('Boston',700,1-05-1999);
insertintoStore_Informationvalues('SanDiego',250,1-05-1999);
insertintoStore_Informationvalues('SanDiego',750,1-05-1999);
SELECTstore_name,SUM(Sales)FROMStore_InformationGROUPBYstore_name
SELECTstore_name,AVG(Sales)FROMStore_InformationGROUPBYstore_name
--HAVING子句子,having子句可以包含计算,比如求和,平均值等
SELECTstore_name,SUM(sales)FROMStore_InformationGROUPBYstore_nameHAVINGSUM(sales)>
1000
SELECTstore_name,SUM(sales)FROMStore_InformationGROUPBYstore_nameHAVINGAVG(sales)>
500
--ALIAS给查询的表格或者栏位取别名
SELECTA1.store_nameStore,SUM(A1.Sales)"TotalSales"FROMStore_InformationA1GROUPBY
A1.store_name
--JOIN关键字
CREATETABLEGeography
(
region_namevarchar(25),
store_namevarchar(25)
);
INSERTINTOGeographyVALUES('EAST','Boston');
INSERTINTOGeographyVALUES('EAST','NewYork');
INSERTINTOGeographyVALUES('WEST','LosAngeles');
INSERTINTOGeographyVALUES('WEST','SanDiego');
select*fromGeography
select*fromStore_Information;
---------------------------------------
SELECTA1.region_nameREGION,SUM(A2.Sales)TOTAL_SALESFROMGeographyA1,Store_InformationA2
WHEREA1.store_name=A2.store_nameGROUPBYA1.region_name
--子查询
SELECTSUM(Sales)FROMStore_InformationWHEREStore_nameIN(SELECTstore_nameFROM
GeographyWHEREregion_name='West')、
--UNION--注意UNION结果集中不显示重复的结果值,就是两个结果集中的并集
SELECTstore_nameFROMStore_Information
UNION
SELECTstore_nameFROMGeography
--unionall可以将结果集中的每一栏资料打印出来,无论是不是重复的结果集
SELECTstore_nameFROMStore
_Information
UNIONALL
SELECTstore_nameFROMGeography
--INTERSECT关键字是结果集中的交集,就是说只有在两个查询结果中都有的才会显示,一个结果中有的不会显示
SELECTstore_nameFROMStore_Information
INTERSECT
SELECTstore_nameFROMGeography
--MINUS关键字
SELECTstore_nameFROMStore_Information
MINUS
SELECTstore_nameFROMGeography
--MINUS指令是运用在两个SQL语句上。它先找出第一个SQL语句所产生的结果,然后看这些结果有没
--有在第二个SQL语句的结果中。如果有的话,那这一笔资料就被去除,而不会在最后的结果中出现。如
--果第二个SQL语句所产生的结果并没有存在于第一个SQL语句所产生的结果内,那这笔资料就被抛弃
SELECTregion_name+''+store_nameConnect_StringFROMGeographyWHEREstore_name='Boston';
--线面这句不适合sqlserver
--SELECTSUBSTRing(store_name,3)FROMGeographyWHEREstore_name='LosAngeles';
SELECTSUBSTRING(store_name,2,4)FROMGeographyWHEREstore_name='LosAngeles';--2是起始位置,4是结束位置
--trim去掉字符串的头部或者尾部空格
SELECTLTRIM('Sample');
SELECTRTRIM('Sample');
--建立视图
CREATEINDEXIDX_store_nameonCUSTOMER(Last_Name)
--join关键字的测试
CREATETABLET_Employee
(
IDINT,
Namenvarchar(50),
RegionIDINT
);
CREATETABLET_Region
(
IDINT,
Namenvarchar(50)
);
INSERTINTOT_EmployeeVALUES(1,'Jimmy',1);
INSERTINTOT_EmployeeVALUES(2,'Rose',2);
INSERTINTOT_EmployeeVALUES(3,'Tom',3);
INSERTINTOT_EmployeeVALUES(4,'Mike',4);
INSERTINTOT_EmployeeVALUES(5,'Mary',5);
select*fromT_Employee
insertintoT_Regionvalues(1,'湖北省');
insertintoT_Regionvalues(2,'上海市');
insertintoT_Regionvalues(3,'广东省');
insertintoT_Regionvalues(4,'山西省');
insertintoT_Regionvalues(5,'杭州市');
insertintoT_Regionvalues(6,'江西省');
select*fromT_Region;
--测试join关键字
select
e.idasEmpId,
e.nameasEmpName,
r.idasRegionId,
r.nameasRegionName
fromT_EmployeeaseJOINT_RegionasrONe.regionId=r.Id
--这里面的as关键字可以去掉,语句如下运行依然正确无比
select
e.idEmpId,
e.nameEmpName,
r.idRegionId,
r.nameRegionName
fromT_EmployeeeJOINT_RegionrONe.regionId=r.Id
--innerjoin关键字、
select
e.idasEmpId,
e.nameasEmpName,
r.idasRegionId,
r.nameasRegionName
fromT_EmployeeaseINNERJOINT_RegionasrONe.regionId=r.Id
select*fromT_Employee
select*fromT_Region;
--deletefromT_EmployeewhereID=1;
--FULLJOIN
select
e.idasEmpId,
e.nameasEmpName,
r.idasRegionId,
r.nameasRegionName
fromT_EmployeeaseFULLJOINT_RegionasrONe.regionId=r.Id